Skip to content

Commit

Permalink
[genetic] genetic creature mutate api
Browse files Browse the repository at this point in the history
  • Loading branch information
Noverish committed May 5, 2024
1 parent b9a1964 commit 2715f7d
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package kim.hyunsub.genetic.bo

import kim.hyunsub.common.web.error.ErrorCode
import kim.hyunsub.common.web.error.ErrorCodeException
import kim.hyunsub.common.web.model.SimpleResponse
import kim.hyunsub.genetic.model.api.ApiGeneticCreature
import kim.hyunsub.genetic.model.api.toApi
import kim.hyunsub.genetic.model.dto.GeneticCreatureParams
import kim.hyunsub.genetic.repository.entity.GeneticCreature
import kim.hyunsub.genetic.repository.mapper.GeneticCreatureMapper
import kim.hyunsub.genetic.repository.mapper.generateId
import org.springframework.stereotype.Service

@Service
class GeneticCreatureMutateBo(
private val geneticCreatureMapper: GeneticCreatureMapper,
) {
fun create(userId: String, params: GeneticCreatureParams): ApiGeneticCreature {
val entity = GeneticCreature(
id = geneticCreatureMapper.generateId(),
userId = userId,
name = params.name,
birthday = params.birthday,
punch = params.punch,
isMale = params.isMale,
type1 = params.type1,
type2 = params.type2,
type3 = params.type3,
type4 = params.type4,
)

geneticCreatureMapper.insert(entity)

return entity.toApi()
}

fun update(userId: String, creatureId: String, params: GeneticCreatureParams): ApiGeneticCreature {
val entity = geneticCreatureMapper.selectOne(creatureId, userId)
?: throw ErrorCodeException(ErrorCode.NOT_FOUND)

val newEntity = entity.copy(
name = params.name,
birthday = params.birthday,
punch = params.punch,
isMale = params.isMale,
type1 = params.type1,
type2 = params.type2,
type3 = params.type3,
type4 = params.type4,
)

geneticCreatureMapper.update(newEntity)

return newEntity.toApi()
}

fun delete(userId: String, creatureId: String): SimpleResponse {
geneticCreatureMapper.deleteOne(creatureId, userId)
return SimpleResponse()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import kim.hyunsub.common.model.ApiPageResult
import kim.hyunsub.common.web.error.ErrorCode
import kim.hyunsub.common.web.error.ErrorCodeException
import kim.hyunsub.genetic.model.api.ApiGeneticCreature
import kim.hyunsub.genetic.model.api.toApi
import kim.hyunsub.genetic.model.dto.GeneticCreatureSearchParams
import kim.hyunsub.genetic.repository.condition.GeneticCreatureCondition
import kim.hyunsub.genetic.repository.entity.GeneticCreature
import kim.hyunsub.genetic.repository.mapper.GeneticCreatureMapper
import org.springframework.data.domain.PageRequest
import org.springframework.stereotype.Service
Expand Down Expand Up @@ -39,16 +39,4 @@ class GeneticCreatureQueryBo(
data = data.map { it.toApi() }
)
}

private fun GeneticCreature.toApi() = ApiGeneticCreature(
id = id,
name = name,
birthday = birthday,
punch = punch,
isMale = isMale,
type1 = type1,
type2 = type2,
type3 = type3,
type4 = type4,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package kim.hyunsub.genetic.controller

import kim.hyunsub.common.web.model.SimpleResponse
import kim.hyunsub.common.web.model.UserAuth
import kim.hyunsub.genetic.bo.GeneticCreatureMutateBo
import kim.hyunsub.genetic.model.api.ApiGeneticCreature
import kim.hyunsub.genetic.model.dto.GeneticCreatureParams
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/api/v1")
class GeneticCreatureMutateController(
private val geneticCreatureMutateBo: GeneticCreatureMutateBo,
) {
@PostMapping("/creatures")
fun create(
user: UserAuth,
@RequestBody params: GeneticCreatureParams,
): ApiGeneticCreature {
return geneticCreatureMutateBo.create(user.idNo, params)
}

@PutMapping("/creatures/{creatureId}")
fun update(
user: UserAuth,
@PathVariable creatureId: String,
@RequestBody params: GeneticCreatureParams,
): ApiGeneticCreature {
return geneticCreatureMutateBo.update(user.idNo, creatureId, params)
}

@DeleteMapping("/creatures/{creatureId}")
fun delete(
user: UserAuth,
@PathVariable creatureId: String,
): SimpleResponse {
return geneticCreatureMutateBo.delete(user.idNo, creatureId)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kim.hyunsub.genetic.model.api

import kim.hyunsub.genetic.repository.entity.GeneticCreature

data class ApiGeneticCreature(
val id: String,
val name: String,
Expand All @@ -11,3 +13,15 @@ data class ApiGeneticCreature(
val type3: Int,
val type4: Int,
)

fun GeneticCreature.toApi() = ApiGeneticCreature(
id = id,
name = name,
birthday = birthday,
punch = punch,
isMale = isMale,
type1 = type1,
type2 = type2,
type3 = type3,
type4 = type4,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package kim.hyunsub.genetic.model.dto

data class GeneticCreatureParams(
val name: String,
val birthday: String,
val punch: String,
val isMale: Boolean,
val type1: Int,
val type2: Int,
val type3: Int,
val type4: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface GeneticCreatureMapper : MapperBase {
fun selectOne(id: String, userId: String? = null): GeneticCreature?
fun insert(entity: GeneticCreature): Int
fun update(entity: GeneticCreature): Int
fun deleteOne(id: String): Int
fun deleteOne(id: String, userId: String? = null): Int
}

fun GeneticCreatureMapper.generateId() = generateId(8)
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@

<update id="update">
UPDATE genetic_creature
SET user_id = ${userId},
SET user_id = #{userId},
birthday = #{birthday},
name = #{name},
punch = #{punch},
Expand All @@ -96,5 +96,6 @@
<delete id="deleteOne">
DELETE FROM genetic_creature
WHERE id = #{id}
<if test="userId != null">AND user_id = #{userId}</if>
</delete>
</mapper>

0 comments on commit 2715f7d

Please sign in to comment.