Skip to content

Commit

Permalink
Merge pull request #26 from jojaeng2/feat/#22-get-follower-following-…
Browse files Browse the repository at this point in the history
…profile-infos

[프로필] [팔로우] 팔로워, 팔로잉 프로필 조회 기능 개발
  • Loading branch information
jojaeng2 authored Sep 20, 2023
2 parents 2e8bdb8 + a202f5c commit 1fc2c6e
Show file tree
Hide file tree
Showing 25 changed files with 688 additions and 155 deletions.
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,26 @@
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")
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

0 comments on commit 1fc2c6e

Please sign in to comment.