Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✏️ Fix : family api service 기능 수정 및 구현 완료 (#81) #85

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/models/family_member_model.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
class FamilyMemberModel {
final int familyMemberId, role, pokeCount, talkCount;
final String name, nickname;
final String name, nickname, introMessage;

FamilyMemberModel.fromJson(Map<String, dynamic> json)
: familyMemberId = json["familyMemberId"],
name = json["name"],
nickname = json["nickname"],
role = json["role"],
pokeCount = json["pokeCount"],
talkCount = json["talkCount"];
talkCount = json["talkCount"],
introMessage = json["introMessage"];
}
34 changes: 25 additions & 9 deletions lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import 'package:flutter/material.dart';
import 'package:fam_story_frontend/style.dart';
import 'package:flutter/services.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import '../models/family_member_model.dart';
import '../services/family_member_api_service.dart';
import 'setting_page.dart';
import 'alarm_page.dart';
import 'package:fam_story_frontend/models/family_model.dart';
import 'package:fam_story_frontend/models/user_model.dart';

Expand Down Expand Up @@ -145,20 +146,16 @@ class _HomePageState extends State<HomePage> {
Row(
children: [
IconButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => AlarmPage()),
);
},
onPressed: () {},
icon: const Icon(Icons.alarm_on),
color: AppColor.swatchColor,
iconSize: 35),
IconButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SettingPage()),
MaterialPageRoute(
builder: (context) => SettingPage()),
);
},
icon: const Icon(Icons.settings),
Expand Down Expand Up @@ -195,7 +192,26 @@ class _HomePageState extends State<HomePage> {
buttonSize: 0.28,
imageSize: 50,
memberImage: 'assets/images/mom.png',
onTap: () {},
onTap: () async {
//FamilyMemberApi 테스트
/*
print(await FamilyMemberApiService.putFamilyMember(
15, 1, "하잉"));
List<FamilyMemberModel> x =
await FamilyMemberApiService.getAllFamilyMember(
13);
for (var member in x) {
print(member.familyMemberId);
print(member.talkCount);
print(member.pokeCount);
print(member.name);
print(member.nickname);
print(member.role);
print(member.introMessage);
}

*/
},
);
},
onAccept: (data) {
Expand Down
42 changes: 30 additions & 12 deletions lib/pages/role_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class RolePage extends StatefulWidget {
State<RolePage> createState() => _SelectRolePageState();
}

class _SelectRolePageState extends State<RolePage> with TickerProviderStateMixin {
class _SelectRolePageState extends State<RolePage>
with TickerProviderStateMixin {
final _familyNameController = TextEditingController();

int? selectedRole;
Expand All @@ -32,7 +33,8 @@ class _SelectRolePageState extends State<RolePage> with TickerProviderStateMixin

@override
Widget build(BuildContext context) {
final args = ModalRoute.of(context)?.settings.arguments as Map<String, dynamic>;
final args =
ModalRoute.of(context)?.settings.arguments as Map<String, dynamic>;
final familyName = args['familyName'] as String;
final familyId = args['familyId'] as int;

Expand All @@ -54,11 +56,17 @@ class _SelectRolePageState extends State<RolePage> with TickerProviderStateMixin
children: [
Text(
"fam'Story",
style: TextStyle(color: AppColor.textColor, fontSize: 40, fontWeight: FontWeight.bold),
style: TextStyle(
color: AppColor.textColor,
fontSize: 40,
fontWeight: FontWeight.bold),
),
Text(
"Tell Me Who You Are !",
style: TextStyle(color: AppColor.textColor, fontSize: 20, fontWeight: FontWeight.bold),
style: TextStyle(
color: AppColor.textColor,
fontSize: 20,
fontWeight: FontWeight.bold),
)
],
),
Expand Down Expand Up @@ -96,7 +104,8 @@ class _SelectRolePageState extends State<RolePage> with TickerProviderStateMixin
const SizedBox(height: 16.0),
GridView.builder(
shrinkWrap: true,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
crossAxisSpacing: 14.0,
mainAxisSpacing: 14.0,
Expand Down Expand Up @@ -184,7 +193,9 @@ class _SelectRolePageState extends State<RolePage> with TickerProviderStateMixin
// TODO: API 연결, 가족 멤버 생성, 가족 ID 가져 오기
print(roleDescriptions[selectedRole!]);
try {
int familyMemberId = await FamilyMemberApiService.postFamilyMember(familyId, selectedRole!);
int familyMemberId =
await FamilyMemberApiService.postFamilyMember(
familyId, selectedRole!, "");
} catch (e) {
print(e.toString());
}
Expand Down Expand Up @@ -212,20 +223,26 @@ class _SelectRolePageState extends State<RolePage> with TickerProviderStateMixin
onPressed: () {
Navigator.of(context).pop(); // 현재 라우트 팝
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => const RootPage()),
MaterialPageRoute(
builder: (context) =>
const RootPage()),
);
},
style: TextButton.styleFrom(
foregroundColor: AppColor.objectColor, // 텍스트 색상
padding: const EdgeInsets.fromLTRB(40, 10, 40, 10), // 버튼 패딩 조정
foregroundColor:
AppColor.objectColor, // 텍스트 색상
padding: const EdgeInsets.fromLTRB(
40, 10, 40, 10), // 버튼 패딩 조정
minimumSize: const Size(100, 40),
backgroundColor: AppColor.textColor, // 버튼 배경색
backgroundColor:
AppColor.textColor, // 버튼 배경색
),
child: const Text(
'Let\'s Go Home',
style: TextStyle(
fontSize: 18, // 버튼 글씨 크기
color: AppColor.objectColor, // 버튼 글씨 색상
color:
AppColor.objectColor, // 버튼 글씨 색상
),
),
),
Expand All @@ -239,7 +256,8 @@ class _SelectRolePageState extends State<RolePage> with TickerProviderStateMixin
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(35),
),
backgroundColor: selectedRole != null ? AppColor.textColor : Colors.grey,
backgroundColor:
selectedRole != null ? AppColor.textColor : Colors.grey,
minimumSize: const Size(120, 40), // selectedRole에 따라 배경색 조절
),
child: Text(
Expand Down
38 changes: 20 additions & 18 deletions lib/services/family_member_api_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:http/http.dart' as http;
import '../models/family_member_model.dart';
import '../models/family_model.dart';

// TODO: 에러 코드 추가
class FamilyMemberApiService {
static const String baseUrl = 'https://famstory.thisiswandol.com/api';

Expand Down Expand Up @@ -49,7 +48,8 @@ class FamilyMemberApiService {
}

/// POST: /family-member [가족 구성원] 가족 구성원 생성
static Future<int> postFamilyMember(int familyId, int role) async {
static Future<int> postFamilyMember(
int familyId, int role, String introMessage) async {
final url = Uri.parse('$baseUrl/family-member');
const storage = FlutterSecureStorage();

Expand All @@ -71,8 +71,12 @@ class FamilyMemberApiService {
'Accept': 'application/json',
'Authorization': 'Bearer $loginToken',
},
body: jsonEncode(
{"familyId": familyId, "role": role, "fcmToken": fcmToken}),
body: jsonEncode({
"familyId": familyId,
"role": role,
"fcmToken": fcmToken,
"introMessage": introMessage
}),
);

// 가족 생성 완료
Expand All @@ -85,7 +89,8 @@ class FamilyMemberApiService {
}

/// PUT: /family-member [가족 구성원] 가족 구성원 역할 수정
static Future<bool> putFamilyMember(int familyMemberId, int role) async {
static Future<bool> putFamilyMember(
int familyMemberId, int role, String introMessage) async {
final url = Uri.parse('$baseUrl/family-member');
const storage = FlutterSecureStorage();

Expand All @@ -98,20 +103,15 @@ class FamilyMemberApiService {
Map<String, dynamic> userInfo = json.decode(userInfoString);
String loginToken = userInfo['token'];

// TODO: 가족 멤버 정보 수정 창에서 null 체크 하기
// 가족 이름만 수정 가능
Map<String, dynamic> data = {};
data.putIfAbsent("familyMemberId", () => familyMemberId);
data.putIfAbsent("role", () => role);

final response = await http.put(
url,
headers: <String, String>{
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer $loginToken',
},
body: jsonEncode(data),
body: jsonEncode(
{"id": familyMemberId, "role": role, "introMessage": introMessage}),
);

if (response.statusCode == 200) {
Expand All @@ -126,7 +126,7 @@ class FamilyMemberApiService {
/// DELETE: /family-member [가족 구성원] 가족 구성원 삭제
static Future<bool> deleteFamilyMember(int familyMemberId) async {
final url =
Uri.parse('$baseUrl/family-member?family-memberId=$familyMemberId');
Uri.parse('$baseUrl/family-member?familyMemberId=$familyMemberId');
const storage = FlutterSecureStorage();

String? userInfoString = await storage.read(key: 'login');
Expand Down Expand Up @@ -157,7 +157,7 @@ class FamilyMemberApiService {
}

/// Get: /family-member [가족 구성원] 구성원 ID를 통한 가족 구성원 정보 반환
static Future<FamilyMemberModel> getFamilyMember(int familyMemberId) async {
static Future<FamilyMemberModel> getFamilyMemberID(int familyMemberId) async {
final url =
Uri.parse('$baseUrl/family-member?family-memberId=$familyMemberId');
const storage = FlutterSecureStorage();
Expand Down Expand Up @@ -194,7 +194,7 @@ class FamilyMemberApiService {
}

/// Get: /family-member/user [가족 구성원] 회원이 속한 가족 구성원 정보 반환
static Future<FamilyMemberModel> getUser() async {
static Future<FamilyMemberModel> getFamilyMember() async {
final url = Uri.parse('$baseUrl/family-member/user');
const storage = FlutterSecureStorage();

Expand All @@ -207,7 +207,7 @@ class FamilyMemberApiService {
Map<String, dynamic> userInfo = json.decode(userInfoString);
String loginToken = userInfo['token'];

final response = await http.delete(
final response = await http.get(
url,
headers: <String, String>{
'Content-Type': 'application/json',
Expand All @@ -230,7 +230,7 @@ class FamilyMemberApiService {
}

/// Post : /family-member/family [가족 구성원] 가족 구성원이 속한 가족의 정보 조회
static Future<FamilyModel> postUser(
static Future<FamilyModel> postAndGetFamily(
int familyMemberId, String fcmToken) async {
final url = Uri.parse('$baseUrl/family-member/family');
const storage = FlutterSecureStorage();
Expand Down Expand Up @@ -297,9 +297,11 @@ class FamilyMemberApiService {
print(jsonDecode(response.body)['message']);
List<dynamic> jsonDataList = jsonDecode(response.body)['data'];
List<FamilyMemberModel> familyMemberList = List<FamilyMemberModel>.from(
jsonDataList.map((item) => FamilyModel.fromJson(item)));
jsonDataList.map((item) => FamilyMemberModel.fromJson(item)));
return familyMemberList;
}
FamilyModel familyInfo =
FamilyModel.fromJson(jsonDecode(response.body)['data']);
// 예외 처리; 메시지를 포함한 예외를 던짐
String errorMessage = jsonDecode(response.body)['message'] ?? 'Error';
print(errorMessage);
Expand Down