20240702 (화) Hercute팀 - 메추리알(MCRA) 코딩 2일차

2024. 7. 2. 17:50카테고리 없음

오늘 작업한 부분
각종 서비스임플

package com.hercute.mcrabe.domain.fridge.service

import com.hercute.mcrabe.domain.fridge.dto.CreateFridgeRequest
import com.hercute.mcrabe.domain.fridge.dto.FridgeResponse
import com.hercute.mcrabe.domain.fridge.dto.UpdateFridgeRequest
import com.hercute.mcrabe.domain.fridge.model.Fridge
import com.hercute.mcrabe.domain.fridge.repository.FridgeRepository
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service

@Service
class FridgeServiceImpl(
    private val fridgeRepository: FridgeRepository
): FridgeService {
    override fun createItemInFridge(memberId: Long, request: CreateFridgeRequest) {
//        val member = Member (
//
//        ) //멤버 엔티티 추가후 수정필요, 카테고리도 추가 필요
        val item = Fridge(
            name = request.name,
            expirationDate = request.expirationDate,
            memo = request.memo,
            storage = request.storage
        )
        fridgeRepository.save(item)
    }

    override fun updateItemOfFridge(memberId: Long, fridgeId: Long, request: UpdateFridgeRequest) {
        //자기 냉장고속 아이템인지 확인하는 절차 필요할지도?
        val item = fridgeRepository.findByIdOrNull(fridgeId)
            ?: throw Exception() //예외처리폴더 추가후 수정 필요
        item.name = request.name
        item.expirationDate = request.expirationDate
        item.memo = request.memo
        item.storage = request.storage
        fridgeRepository.save(item)
    }

    override fun deleteItemOfFridge(memberId: Long, fridgeId: Long) {
        val item = fridgeRepository.findByIdOrNull(fridgeId)
            ?: throw Exception() //예외처리폴더 추가후 수정 필요
        fridgeRepository.delete(item)
    }

    override fun getItemOfFridge(memberId: Long, fridgeId: Long): FridgeResponse {
        val item = fridgeRepository.findByIdOrNull(fridgeId)
            ?: throw Exception() //예외처리폴더 추가후 수정 필요
        return FridgeResponse.from(item)
    }

    override fun getItemListOfFridge(memberId: Long): List<FridgeResponse> {
//        val itemList = fridgeRepository.findByMemberId(memberId)
        TODO()
    }
}
package com.hercute.mcrabe.domain.cart.service

import com.hercute.mcrabe.domain.cart.dto.AddItemInCartRequest
import com.hercute.mcrabe.domain.cart.dto.ItemResponse
import com.hercute.mcrabe.domain.cart.dto.UpdateItemInCartRequest
import com.hercute.mcrabe.domain.cart.model.Cart
import com.hercute.mcrabe.domain.cart.repository.CartRepository
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import java.sql.Timestamp

@Service
class CartServiceImpl(
    private val cartRepository: CartRepository
):CartService {
    override fun addItemInCart(memberId: Long, request: AddItemInCartRequest) {
        val item = Cart(
            name = request.name,
            memo = request.memo
            //카테고리 추가 필요
        )
        cartRepository.save(item)
    }

    override fun updateItemOfCart(memberId: Long, itemId: Long, request: UpdateItemInCartRequest) {
        val item = cartRepository.findByIdOrNull(itemId)
            ?: throw Exception()
        item.name = request.name
        item.memo = request.memo
    }

    override fun deleteItemOfCart(memberId: Long, itemId: Long) {
        val item = cartRepository.findByIdOrNull(itemId)
            ?: throw Exception()
        cartRepository.delete(item)
    }

    override fun getItemOfCart(memberId: Long, itemId: Long): ItemResponse {
        val item = cartRepository.findByIdOrNull(itemId)
            ?: throw Exception()
        return ItemResponse.from(item)
    }

    override fun getItemList(memberId: Long, purchasedDate: Timestamp?): List<ItemResponse> {
        TODO("Not yet implemented")
    }

    override fun getCartRecords(memberId: Long): List<ItemResponse> {
        TODO("Not yet implemented")
    }

    override fun checkItemPurchaseStatus(memberId: Long, itemId: Long) {
        TODO("Not yet implemented")
    }

    override fun moveItemToFridge(memberId: Long) {
        TODO("Not yet implemented")
    }


}

 

글로벌예외핸들러

package com.hercute.mcrabe.global.error.exception

import com.hercute.mcrabe.global.error.exception.dto.ErrorResponse
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice
import kotlin.io.AccessDeniedException

@RestControllerAdvice
class GlobalExceptionHandler {

    @ExceptionHandler
    fun handleRuntimeException(e: RuntimeException): ResponseEntity<ErrorResponse> {
        e.printStackTrace()
        return ResponseEntity
            .status(HttpStatus.INTERNAL_SERVER_ERROR)
            .body(ErrorResponse(message = "Internal error"))
    }

    @ExceptionHandler(ModelNotFoundException::class)
    fun handleModelNotFoundException(e: ModelNotFoundException): ResponseEntity<ErrorResponse> {
        return ResponseEntity
            .status(HttpStatus.NOT_FOUND)
            .body(ErrorResponse(e.message))
    }

    @ExceptionHandler(AccessDeniedException::class)
    fun handleAccessDeniedException(e: AccessDeniedException): ResponseEntity<ErrorResponse> {
        return ResponseEntity
            .status(HttpStatus.NOT_FOUND)
            .body(ErrorResponse(e.message))
    }

    @ExceptionHandler(InvalidCredentialException::class)
    fun handleInvalidCredentialException(e: InvalidCredentialException): ResponseEntity<ErrorResponse> {
        return ResponseEntity
            .status(HttpStatus.NOT_FOUND)
            .body(ErrorResponse(e.message))
    }
}

 

 

이 외에는 스웨거 콘픽, 의존성 설정, 예외 처리 할 부분 예상 등이 있다.