Skip to content

Commit

Permalink
Merge pull request #2 from face-gram/hotfix/history
Browse files Browse the repository at this point in the history
fix: multipart/form-data json, history service annotation
  • Loading branch information
jin-jae authored Mar 31, 2023
2 parents ac89cf4 + 249b2d1 commit 2e2d1e9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.facegram.facegrambackend.controller.face

import com.facegram.facegrambackend.dto.request.analysis.AnalysisLowCreateRequestDto
import com.facegram.facegrambackend.dto.request.analysis.description.DescriptionDto
import com.facegram.facegrambackend.dto.request.analysis.info.InfoDto
import com.facegram.facegrambackend.security.CustomUserDetails
import com.facegram.facegrambackend.service.analysis.AnalysisService
import org.springframework.http.ResponseEntity
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RequestPart
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.multipart.MultipartFile
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

Expand All @@ -18,12 +23,16 @@ class AnalysisController(
private val analysisService: AnalysisService
) {

@PostMapping("/")
fun createAnalysis(@RequestBody analysisLowCreateRequestDto: AnalysisLowCreateRequestDto,
@AuthenticationPrincipal user : CustomUserDetails,
request: HttpServletRequest,
response: HttpServletResponse
@PostMapping("/", consumes = ["multipart/form-data"])
fun createAnalysis(
@RequestPart info: InfoDto,
@RequestPart description: DescriptionDto,
@RequestPart image: MultipartFile,
@AuthenticationPrincipal user : CustomUserDetails,
request: HttpServletRequest,
response: HttpServletResponse
): ResponseEntity<Any> {
return analysisService.createAnalysis(analysisLowCreateRequestDto,user,request,response)
val analysisRequest = AnalysisLowCreateRequestDto(info,description,image)
return analysisService.createAnalysis(analysisRequest,user,request,response)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import kotlin.math.log


@Service
@Slf4j
@RestController
@RequestMapping("/history")
class HistoryController constructor(
private val historyService: HistoryService,
Expand All @@ -26,7 +26,9 @@ class HistoryController constructor(
): MutableList<UserHistoryAnalysisDto> {
println("컨트롤러 도착")
val userId: Long = user.name.toLong()
return historyService.historySearchByUser(userId)
val historySearchByUser = historyService.historySearchByUser(userId)
println("history 배열 가져옴")
return historySearchByUser
}

@DeleteMapping("/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface AnalysisRepository: JpaRepository<Analysis,Long>{
fun findAllById(id: Long)


fun findAllByUser(user: Optional<User>): List<Analysis>
fun findAllByUser(user:User): List<Analysis>

fun deleteAnalysisById(id: Long)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ class GCSService constructor(
val ext = image.contentType // 파일의 형식, 확장자

// Cloud에 이미지 업로드
val blobInfo:BlobInfo = storage.create(
// val blobInfo:BlobInfo =
storage.create(
BlobInfo.newBuilder(bucketName, uuid)
.setContentType(ext)
.build(),
image.inputStream
)
return "https://storage.googleapis.com/${bucketName}/${uuid}.${ext}"
return "https://storage.googleapis.com/${bucketName}/${uuid}"//.${ext}"
}
//클라가 저장했던 이미지를 다시 보내 달라할 땐 사실 굉장히 간단하고 쉽습니다!
//저희가 DB에 String 타입으로 저장해둔 UUID값을 보내주기만 하면 된답니당 ㅎㅎ
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import com.facegram.facegrambackend.domain.user.UserRepository
import com.facegram.facegrambackend.domain.wrinkle.Wrinkle
import com.facegram.facegrambackend.domain.wrinkle.WrinkleRepository
import com.facegram.facegrambackend.dto.request.analysis.AnalysisLowCreateRequestDto
import com.facegram.facegrambackend.dto.request.analysis.description.DescriptionDto
import com.facegram.facegrambackend.dto.request.analysis.info.InfoDto
import com.facegram.facegrambackend.dto.response.Message
import com.facegram.facegrambackend.dto.response.ResponseType
import com.facegram.facegrambackend.gcp.storage.GCSService
Expand All @@ -32,6 +34,8 @@ import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.multipart.MultipartFile
import java.lang.IllegalArgumentException
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

Expand Down Expand Up @@ -64,10 +68,13 @@ class AnalysisService constructor(

@Transactional
fun createAnalysis(
analysisLowCreateRequestDto: AnalysisLowCreateRequestDto,
user: CustomUserDetails,
request: HttpServletRequest,
response: HttpServletResponse
analysisLowCreateRequestDto: AnalysisLowCreateRequestDto,
// image: MultipartFile,
// info: InfoDto,
// image: MultipartFile,
user: CustomUserDetails,
request: HttpServletRequest,
response: HttpServletResponse
)
:ResponseEntity<Any>{
val face = faceRepository.save(createFace(analysisLowCreateRequestDto))
Expand All @@ -80,15 +87,20 @@ class AnalysisService constructor(
// val characteristic =
val feature = featureRepository.save(createFeature(analysisLowCreateRequestDto))
val impression = impressionRepository.save(createImpression(analysisLowCreateRequestDto))
val findUser = userRepository.findByUsername(user.username)?: throw IllegalArgumentException("없는 유저입니다.")
println("다른 것들 생성 완료")
val findUser = userRepository.findById(user.name.toLong())
if(findUser.isEmpty){
throw IllegalArgumentException("유저를 찾을 수 없습니다.")
}
println("유저 생성 완료")
val imageUrl = gcsService.uploadFileToGCS(analysisLowCreateRequestDto.image)

numIncrease()

val analysis = Analysis.newInstance(
null,
"${user.username}님이 생성한 몽타주 $analysisNum",
findUser,
findUser.get(),
face,
hairstyle,
eyebrows,
Expand All @@ -110,9 +122,6 @@ class AnalysisService constructor(

val message = Message(ResponseType.OK,"성공입니다.")

authService.refreshToken(response,request,
request.getHeader("Authorization"))

return responseEntityService.createResponseEntity(message,HttpStatus.OK)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.facegram.facegrambackend.dto.response.history.analysishistory.analysi
import com.facegram.facegrambackend.dto.response.history.userhistory.UserHistoryAnalysisDto
import com.facegram.facegrambackend.security.CustomUserDetails
import com.facegram.facegrambackend.service.responseentity.ResponseEntityService
import lombok.extern.slf4j.Slf4j
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Service
Expand All @@ -20,6 +21,7 @@ import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import kotlin.IllegalArgumentException

@Slf4j
@Service
class HistoryService
constructor(
Expand All @@ -31,12 +33,15 @@ class HistoryService
@Transactional
fun historySearchByUser(userId: Long)
: MutableList<UserHistoryAnalysisDto>{
println("history 서치")
val history: MutableList<UserHistoryAnalysisDto> = mutableListOf()
val findUser: Optional<User> = userRepository.findById(userId)
if(findUser.isEmpty){
throw IllegalArgumentException("유저를 찾지 못했습니다.")
}
val userAnalysisHistory: List<Analysis> = analysisRepository.findAllByUser(findUser)
println("유저 서치")
val userAnalysisHistory: List<Analysis> = analysisRepository.findAllByUser(findUser.get())
println("history 서치")
userAnalysisHistory.forEach {
analysis ->
history.add(
Expand All @@ -46,6 +51,8 @@ class HistoryService
analysis.createdAt)
)
}
println(history)
println("히스토리 리턴")
return history
}

Expand Down

0 comments on commit 2e2d1e9

Please sign in to comment.