Skip to content

Commit

Permalink
Create user edit api
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhello0507 committed Oct 7, 2024
1 parent 0b78a2a commit 98faed9
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.bestswlkh0310.graduating.graduatingserver.api.school
package com.bestswlkh0310.graduating.graduatingserver.api.meal

import com.bestswlkh0310.graduating.graduatingserver.api.meal.MealService
import org.springframework.web.bind.annotation.*


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class UserController(
private val userService: UserService
) {
@GetMapping("me")
fun getMe() = userService.getMe()

fun getMe() =
userService.getMe()

@PatchMapping
fun editUser(@Valid @RequestBody req: EditUserReq) = userService.editUser(req)
fun editUser(@Valid @RequestBody req: EditUserReq) =
userService.editUser(req)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,30 @@ package com.bestswlkh0310.graduating.graduatingserver.api.user
import com.bestswlkh0310.graduating.graduatingserver.api.core.VoidRes
import com.bestswlkh0310.graduating.graduatingserver.api.user.dto.EditUserReq
import com.bestswlkh0310.graduating.graduatingserver.api.user.dto.UserRes
import com.bestswlkh0310.graduating.graduatingserver.core.school.SchoolRepository
import com.bestswlkh0310.graduating.graduatingserver.core.school.getBy
import com.bestswlkh0310.graduating.graduatingserver.core.user.UserAuthenticationHolder
import com.bestswlkh0310.graduating.graduatingserver.core.user.UserRepository
import org.springframework.stereotype.Service

@Service
class UserService(
private val userRepository: UserRepository,
private val userAuthenticationHolder: UserAuthenticationHolder
private val userAuthenticationHolder: UserAuthenticationHolder,
private val schoolRepository: SchoolRepository
) {
fun getMe(): UserRes = UserRes.of(
userAuthenticationHolder.current()
)

fun editUser(req: EditUserReq): VoidRes {
val user = userAuthenticationHolder.current()
user.update(req.nickname)
val school = req.schoolId?.let(schoolRepository::getBy)
user.update(
nickname = req.nickname,
graduatingYear = req.graduatingYear,
school = school
)
userRepository.save(user)
return VoidRes()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ import jakarta.validation.constraints.Size

data class EditUserReq(
@Size(min = 1, max = 24)
val nickname: String,
val nickname: String?,
val graduatingYear: Int,
val schoolId: Long?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,19 @@ class UserEntity(
this.school = school
}

fun update(nickname: String) {
this.nickname = nickname
fun update(
nickname: String?,
graduatingYear: Int?,
school: SchoolEntity?
) {
if (nickname != null) {
this.nickname = nickname
}
if (graduatingYear != null) {
this.graduatingYear = graduatingYear
}
if (school != null) {
this.school = school
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import SwiftUI

import MyDesignSystem

struct EditSchoolPath: Hashable {}

struct EditSchoolView: View {
struct Path: Hashable {}
@EnvironmentObject private var router: Router

@StateObject private var viewModel = SearchSchoolViewModel()

init(_ path: EditSchoolPath) {}
private let path: Path
init(path: Path) {
self.path = path
}
}

extension EditSchoolView {
Expand All @@ -20,7 +21,10 @@ extension EditSchoolView {
.myFont(.title1B)
.foreground(Colors.Label.normal)
.frame(maxWidth: .infinity, alignment: .leading)
SearchSchoolContainer(for: viewModel.searchedSchools, searchText: $viewModel.searchSchoolName) { _ in
SearchSchoolContainer(
for: viewModel.searchedSchools,
searchText: $viewModel.searchSchoolName
) { _ in
router.toRoot()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ struct MainCoordinator: View {

var body: some View {
MainView()
.navigationDestination(for: EditSchoolPath.self) { EditSchoolView($0) }
.navigationDestination(for: EditSchoolView.Path.self) { EditSchoolView(path: $0) }
.navigationDestination(for: EditGradePath.self) { EditGradeView($0) }
.navigationDestination(for: SettingPath.self) { _ in SettingView() }
.navigationDestination(for: EditProfilePath.self) { _ in EditProfileView() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ extension ProfileView: View {
.padding(.top, 16)
HStack(spacing: 12) {
MyButton("학교 수정", role: .assistive, expanded: true) {
router.push(EditSchoolPath())
router.push(EditSchoolView.Path())
}
.size(.medium)
.frame(maxWidth: .infinity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ extension OnboardingSecondView: View {
}
.padding(insets)
}
.background(Colors.Background.neutral)
}
}

Expand Down

0 comments on commit 98faed9

Please sign in to comment.