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

[프로필] [팔로우] 팔로워, 팔로잉 프로필 조회 기능 개발 #26

Merged
merged 9 commits into from
Sep 20, 2023
6 changes: 6 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 관련 이슈

# 변경 사항
1.

# 참고 사항
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package joryu.sns_service.follow.controller

import joryu.sns_service.follow.dto.request.FollowRequest
import joryu.sns_service.follow.dto.request.UnFollowRequest
import joryu.sns_service.follow.service.FollowService
import lombok.RequiredArgsConstructor
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/follow")
@RequiredArgsConstructor
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RequiredArgsConstructor는 없어도 될 것 같습니당

class FollowController(
private val followService: FollowService
) {
@PostMapping("/{fromProfileId}")
fun follow(@PathVariable("fromProfileId") fromProfileId: Long, @RequestBody followRequest: FollowRequest): ResponseEntity<Void> {
followService.follow(fromProfileId, followRequest.toProfileId)
return ResponseEntity.ok().build()
}

@DeleteMapping("/{fromProfileId}")
fun unFollow(@PathVariable("fromProfileId") fromProfileId: Long, @RequestBody unFollowRequest: UnFollowRequest): ResponseEntity<Void> {
followService.unFollow(fromProfileId, unFollowRequest.toProfileId)
return ResponseEntity.ok().build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package joryu.sns_service.follow.dto.request

data class FollowRequest(val toProfileId: Long) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package joryu.sns_service.follow.dto.request

data class UnFollowRequest(val toProfileId: Long) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package joryu.sns_service.follow.entity

import jakarta.persistence.*
import joryu.sns_service.common.entity.BaseEntity
import joryu.sns_service.profile.entity.Profile

@Table(name = "follow")
@Entity
class Follow(
@Id
@Column(name = "follow_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long?,

@ManyToOne
@JoinColumn
val fromProfile: Profile?,

@ManyToOne
@JoinColumn
val toProfile: Profile?
) : BaseEntity() {
constructor() : this(null, null, null)
constructor(fromProfile: Profile, toProfile: Profile) : this(null, fromProfile, toProfile) {
toProfile.addFollower(this)
fromProfile.addFollowing(this)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package joryu.sns_service.follow.repository

import joryu.sns_service.follow.entity.Follow
import joryu.sns_service.profile.entity.Profile
import org.springframework.data.jpa.repository.JpaRepository

interface FollowRepository : JpaRepository<Follow, Long> {
fun deleteByFromProfileAndToProfile(fromProfile: Profile, toProfile: Profile)
fun findAllByToProfile(toProfile: Profile): MutableList<Follow>
fun findAllByFromProfile(fromProfile: Profile): MutableList<Follow>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package joryu.sns_service.follow.service

import joryu.sns_service.follow.entity.Follow
import joryu.sns_service.follow.repository.FollowRepository
import joryu.sns_service.profile.exception.ProfileBaseException
import joryu.sns_service.profile.exception.ProfileExceptionEnums
import joryu.sns_service.profile.repository.ProfileRepository
import lombok.RequiredArgsConstructor
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@RequiredArgsConstructor
class FollowService(
private val followRepository: FollowRepository,
private val profileRepository: ProfileRepository
) {

@Transactional
fun follow(fromProfileId: Long, toProfileId: Long) {
val fromProfile = profileRepository.findById(fromProfileId)
.orElseThrow { ProfileBaseException(ProfileExceptionEnums.PROFILE_NOT_FOUND) }
val toProfile = profileRepository.findById(toProfileId)
.orElseThrow { ProfileBaseException(ProfileExceptionEnums.PROFILE_NOT_FOUND) }
val follow = Follow(fromProfile, toProfile)
followRepository.save(follow)
}

@Transactional
fun unFollow(fromProfileId: Long, toProfileId: Long) {
val fromProfile = profileRepository.findById(fromProfileId)
.orElseThrow { ProfileBaseException(ProfileExceptionEnums.PROFILE_NOT_FOUND) }
val toProfile = profileRepository.findById(toProfileId)
.orElseThrow { ProfileBaseException(ProfileExceptionEnums.PROFILE_NOT_FOUND) }
followRepository.deleteByFromProfileAndToProfile(fromProfile, toProfile)
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,54 @@ package joryu.sns_service.profile.controller

import joryu.sns_service.profile.dto.request.ProfileCreateRequest
import joryu.sns_service.profile.dto.request.ProfileUpdateRequest
import joryu.sns_service.profile.dto.response.AllProfileResponse
import joryu.sns_service.profile.dto.response.ProfileInfoResponse
import joryu.sns_service.profile.entity.Profile
import joryu.sns_service.profile.service.ProfileService
import lombok.RequiredArgsConstructor
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.*

@Controller
@RestController
@RequestMapping("/profile")
@RequiredArgsConstructor
class ProfileApiController(
private val profileService: ProfileService
) {

@PostMapping()
fun createProfile(@RequestBody profileCreateRequest: ProfileCreateRequest): ResponseEntity<Profile> {
val profile = profileService.create(profileCreateRequest)
return ResponseEntity(profile, HttpStatus.OK)
fun createProfile(@RequestBody profileCreateRequest: ProfileCreateRequest): ResponseEntity<ProfileInfoResponse> {
val profileInfo = profileService.create(profileCreateRequest)
return ResponseEntity(profileInfo, HttpStatus.OK)
}

@GetMapping("/{id}")
fun findProfile(@PathVariable id: Long): ResponseEntity<ProfileInfoResponse> {
val profile = profileService.findOneById(id)
return ResponseEntity(profile, HttpStatus.OK)
val profileInfo = profileService.findOneById(id)
return ResponseEntity(profileInfo, HttpStatus.OK)
}

@PutMapping("/{id}")
fun updateProfile(@PathVariable id: Long, @RequestBody profileUpdateRequest: ProfileUpdateRequest): ResponseEntity<ProfileInfoResponse> {
val profile = profileService.update(id, profileUpdateRequest)
return ResponseEntity(profile, HttpStatus.OK)
val profileInfo = profileService.update(id, profileUpdateRequest)
return ResponseEntity(profileInfo, HttpStatus.OK)
}

@DeleteMapping("/{id}")
fun deleteProfile(@PathVariable id: Long): ResponseEntity<Any> {
profileService.delete(id)
return ResponseEntity.ok().build()
}

@GetMapping("/follower/{id}")
fun findAllFollowerProfiles(@PathVariable id: Long): ResponseEntity<AllProfileResponse> {
val profiles = profileService.findAllFollowerProfileInfo(id)
return ResponseEntity(profiles, HttpStatus.OK)
}

@GetMapping("/following/{id}")
fun findAllFollowingProfiles(@PathVariable id: Long): ResponseEntity<AllProfileResponse> {
val profiles = profileService.findAllFollowingProfileInfo(id)
return ResponseEntity(profiles, HttpStatus.OK)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package joryu.sns_service.profile.dto.response

import joryu.sns_service.profile.entity.Profile

data class AllProfileResponse(
val profileInfo: MutableList<ProfileInfoResponse> = mutableListOf()
) {

fun addProfileInfoInFollows(profiles: List<Profile?>): AllProfileResponse {
profiles.forEach { profile -> profileInfo.add(ProfileInfoResponse(profile)) }
return this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package joryu.sns_service.profile.dto.response

import joryu.sns_service.profile.entity.Profile

data class ProfileInfoResponse(val name: String) {
constructor(profile: Profile): this(profile.name)
data class ProfileInfoResponse(
val name: String?,
val followerNumber: Int?,
val followingNumber: Int?
) {
constructor(profile: Profile?) : this(profile?.name, profile?.followerNumber, profile?.followingNumber)
}
Loading