-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] Category Domain 정의 및 CRUD 개발 (#18)
- Loading branch information
Showing
20 changed files
with
678 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
src/main/kotlin/com/mjucow/eatda/domain/store/category/Category.kt
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/com/mjucow/eatda/domain/store/entity/Category.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.mjucow.eatda.domain.store.entity | ||
|
||
import com.mjucow.eatda.domain.common.BaseEntity | ||
import jakarta.persistence.Column | ||
import jakarta.persistence.Entity | ||
import jakarta.persistence.Table | ||
|
||
@Entity | ||
@Table(name = "category") | ||
class Category() : BaseEntity() { | ||
constructor(name: String) : this() { | ||
validateName(name) | ||
this.name = name | ||
} | ||
|
||
@Column(nullable = false, unique = true) | ||
var name: String = "" | ||
set(value) { | ||
validateName(value) | ||
field = value | ||
} | ||
|
||
private fun validateName(name: String) { | ||
require(name.isNotBlank() && name.trim().length <= MAX_NAME_LENGTH) | ||
} | ||
companion object { | ||
const val MAX_NAME_LENGTH = 31 | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/kotlin/com/mjucow/eatda/domain/store/service/command/CategoryCommandService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.mjucow.eatda.domain.store.service.command | ||
|
||
import com.mjucow.eatda.domain.store.entity.Category | ||
import com.mjucow.eatda.domain.store.service.command.dto.CreateCommand | ||
import com.mjucow.eatda.domain.store.service.command.dto.UpdateNameCommand | ||
import com.mjucow.eatda.persistence.store.CategoryRepository | ||
import jakarta.transaction.Transactional | ||
import org.springframework.data.repository.findByIdOrNull | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
@Transactional | ||
class CategoryCommandService( | ||
private val repository: CategoryRepository, | ||
) { | ||
fun create(command: CreateCommand): Long { | ||
val newName = command.name.trim() | ||
require(repository.existsByName(newName).not()) | ||
|
||
return repository.save(Category(newName)).id | ||
} | ||
|
||
fun updateName(id: Long, command: UpdateNameCommand) { | ||
val domain = getById(id) | ||
|
||
val newName = command.name.trim() | ||
if (domain.name == newName) { | ||
return | ||
} | ||
domain.name = newName | ||
|
||
repository.save(domain) | ||
} | ||
|
||
fun delete(id: Long) { | ||
val domain = repository.findByIdOrNull(id) ?: return | ||
repository.delete(domain) | ||
} | ||
|
||
private fun getById(id: Long): Category { | ||
return repository.findByIdOrNull(id) ?: throw IllegalArgumentException() | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/main/kotlin/com/mjucow/eatda/domain/store/service/command/dto/CreateCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.mjucow.eatda.domain.store.service.command.dto | ||
|
||
data class CreateCommand(val name: String) |
3 changes: 3 additions & 0 deletions
3
src/main/kotlin/com/mjucow/eatda/domain/store/service/command/dto/UpdateNameCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.mjucow.eatda.domain.store.service.command.dto | ||
|
||
data class UpdateNameCommand(val name: String) |
20 changes: 20 additions & 0 deletions
20
src/main/kotlin/com/mjucow/eatda/domain/store/service/query/CategoryQueryService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.mjucow.eatda.domain.store.service.query | ||
|
||
import com.mjucow.eatda.domain.store.service.query.dto.Categories | ||
import com.mjucow.eatda.domain.store.service.query.dto.CategoryDto | ||
import com.mjucow.eatda.persistence.store.CategoryRepository | ||
import org.springframework.data.repository.findByIdOrNull | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class CategoryQueryService( | ||
val repository: CategoryRepository, | ||
) { | ||
fun findAll(): Categories { | ||
return Categories(repository.findAll().map(CategoryDto::from)) | ||
} | ||
|
||
fun findById(id: Long): CategoryDto? { | ||
return repository.findByIdOrNull(id)?.run { CategoryDto.from(this) } | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/com/mjucow/eatda/domain/store/service/query/dto/Categories.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.mjucow.eatda.domain.store.service.query.dto | ||
|
||
data class Categories( | ||
val categoryList: List<CategoryDto>, | ||
) : ArrayList<CategoryDto>(categoryList) |
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/com/mjucow/eatda/domain/store/service/query/dto/CategoryDto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.mjucow.eatda.domain.store.service.query.dto | ||
|
||
import com.mjucow.eatda.domain.store.entity.Category | ||
|
||
data class CategoryDto( | ||
val id: Long, | ||
val name: String, | ||
) { | ||
companion object { | ||
fun from(domain: Category): CategoryDto { | ||
return CategoryDto(domain.id, domain.name) | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/com/mjucow/eatda/persistence/store/CategoryRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.mjucow.eatda.persistence.store | ||
|
||
import com.mjucow.eatda.domain.store.entity.Category | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface CategoryRepository : JpaRepository<Category, Long> { | ||
fun existsByName(name: String): Boolean | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/kotlin/com/mjucow/eatda/presentation/store/category/CategoryController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.mjucow.eatda.presentation.store.category | ||
|
||
import com.mjucow.eatda.domain.store.service.command.CategoryCommandService | ||
import com.mjucow.eatda.domain.store.service.command.dto.CreateCommand | ||
import com.mjucow.eatda.domain.store.service.command.dto.UpdateNameCommand | ||
import com.mjucow.eatda.domain.store.service.query.CategoryQueryService | ||
import com.mjucow.eatda.domain.store.service.query.dto.Categories | ||
import com.mjucow.eatda.domain.store.service.query.dto.CategoryDto | ||
import com.mjucow.eatda.presentation.common.ApiResponse | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.web.bind.annotation.DeleteMapping | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PatchMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
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.ResponseStatus | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/categories") | ||
class CategoryController( | ||
val categoryQueryService: CategoryQueryService, | ||
val categoryCommandService: CategoryCommandService, | ||
) { | ||
@GetMapping | ||
@ResponseStatus(HttpStatus.OK) | ||
fun findAll(): ApiResponse<Categories> { | ||
return ApiResponse.success(categoryQueryService.findAll()) | ||
} | ||
|
||
@PostMapping | ||
@ResponseStatus(HttpStatus.CREATED) | ||
fun create(@RequestBody command: CreateCommand): ApiResponse<Long> { | ||
val id = categoryCommandService.create(command) | ||
return ApiResponse.success(id) | ||
} | ||
|
||
@GetMapping("/{categoryId}") | ||
@ResponseStatus(HttpStatus.OK) | ||
fun findById(@PathVariable("categoryId") id: Long): ApiResponse<CategoryDto> { | ||
return ApiResponse.success(categoryQueryService.findById(id)) | ||
} | ||
|
||
@DeleteMapping("/{categoryId}") | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
fun deleteById(@PathVariable("categoryId") id: Long) { | ||
categoryCommandService.delete(id) | ||
} | ||
|
||
@PatchMapping("/{categoryId}") | ||
@ResponseStatus(HttpStatus.OK) | ||
fun updateNameById( | ||
@PathVariable("categoryId") id: Long, | ||
@RequestBody command: UpdateNameCommand, | ||
) { | ||
categoryCommandService.updateName(id, command) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
-- liquibase formatted sql | ||
|
||
-- changeset liquibase:1 | ||
CREATE TABLE Category ( | ||
id bigint NOT NULL, | ||
name varchar(31) NOT NULL, | ||
created_at timestamp with time zone NOT NULL, | ||
updated_at timestamp with time zone NOT NULL | ||
CREATE TABLE category ( | ||
id bigserial NOT NULL, | ||
name varchar(31) NOT NULL, | ||
created_at timestamp NOT NULL, | ||
updated_at timestamp NOT NULL | ||
); | ||
|
||
ALTER TABLE Category ADD CONSTRAINT PK_CATEGORY PRIMARY KEY (id); | ||
ALTER TABLE category ADD CONSTRAINT PK_CATEGORY PRIMARY KEY (id); | ||
CREATE UNIQUE INDEX idx_category_name ON category(name); |
24 changes: 24 additions & 0 deletions
24
src/test/kotlin/com/mjucow/eatda/domain/AbstractDataTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.mjucow.eatda.domain | ||
|
||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest | ||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection | ||
import org.springframework.test.context.ActiveProfiles | ||
import org.testcontainers.containers.PostgreSQLContainer | ||
import org.testcontainers.junit.jupiter.Container | ||
import org.testcontainers.junit.jupiter.Testcontainers | ||
|
||
@DataJpaTest | ||
@Testcontainers | ||
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) | ||
@ActiveProfiles("test") | ||
abstract class AbstractDataTest { | ||
companion object { | ||
@Container | ||
@ServiceConnection | ||
val POSTGRESQL_CONTAINER = PostgreSQLContainer("postgres:16.0-alpine") | ||
.withUsername("test-user") | ||
.withPassword("test-password") | ||
.withDatabaseName("test_db")!! | ||
} | ||
} |
Oops, something went wrong.