Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/admin concert place #2

Merged
merged 10 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions admin/admin-concert/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
dependencies {
// database
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.mysql:mysql-connector-j'

// validator
implementation 'org.springframework.boot:spring-boot-starter-validation'

}

bootJar {
archivesBaseName = 'Admin-concert'
archiveFileName = 'admin-concert.jar'
destinationDirectory = file(project.rootProject.projectDir)
enabled = true
}

jar {
enabled = false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package co.kr.ticketing.adminconcert;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@EnableJpaAuditing
@SpringBootApplication
public class AdminConcertApplication {

public static void main(String[] args) {
SpringApplication.run(AdminConcertApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package co.kr.ticketing.adminconcert.common.dto;

import lombok.Builder;

@Builder
public record CreatedDto(
Long id
) {
public static CreatedDto from(Long id) {
return CreatedDto.builder()
.id(id)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package co.kr.ticketing.adminconcert.common.dto;

import lombok.Getter;

@Getter
public class ResponseDto<T> {
private final String code;
private final T body;

public ResponseDto(String code) {
this.code = code;
this.body = null;
}

public ResponseDto(String code, T body) {
this.code = code;
this.body = body;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package co.kr.ticketing.adminconcert.common.exception;

import org.springframework.http.HttpStatus;

import lombok.Getter;

@Getter
public class CustomException extends RuntimeException {
private final HttpStatus status;

public CustomException(final HttpStatus status, final String message) {
super(message);
this.status = status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package co.kr.ticketing.adminconcert.common.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.CONFLICT)
public class DuplicateException extends CustomException {
public DuplicateException(String message) {
super(HttpStatus.CONFLICT, message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package co.kr.ticketing.adminconcert.common.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import co.kr.ticketing.adminconcert.common.dto.ResponseDto;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {
@ExceptionHandler(CustomException.class)
public ResponseEntity<ResponseDto<String>> customException(CustomException exception) {
ResponseDto<String> responseDto = new ResponseDto<>(exception.getStatus().toString(),
exception.getMessage());
log.error("CustomRuntimeException 발생 : code: {}, message: {}", responseDto.getCode(), responseDto.getBody());
return new ResponseEntity<>(responseDto, exception.getStatus());
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ResponseDto<String>> customException(MethodArgumentNotValidException exception) {
HttpStatus badRequest = HttpStatus.BAD_REQUEST;
ResponseDto<String> responseDto = new ResponseDto<>(badRequest.toString(), "요청 값이 잘 못 됐습니다");
log.error("MethodArgumentNotValidException 발생 : message: {}", exception.getMessage());
return new ResponseEntity<>(responseDto, badRequest);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package co.kr.ticketing.adminconcert.common.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.NO_CONTENT)
public class ResourceNotFoundException extends CustomException {
public ResourceNotFoundException(String domain, Long id) {
super(HttpStatus.NO_CONTENT, "domain: " + domain + ", id: " + id);
}

public ResourceNotFoundException(String domain, String id) {
super(HttpStatus.NO_CONTENT, "domain: " + domain + ", id: " + id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package co.kr.ticketing.adminconcert.place.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
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.RestController;

import co.kr.ticketing.adminconcert.common.dto.CreatedDto;
import co.kr.ticketing.adminconcert.common.dto.ResponseDto;
import co.kr.ticketing.adminconcert.place.controller.request.AddPlaceRequest;
import co.kr.ticketing.adminconcert.place.controller.request.GetPlaceRequest;
import co.kr.ticketing.adminconcert.place.controller.response.GetPlaceResponse;
import co.kr.ticketing.adminconcert.place.controller.response.PlaceResponseCode;
import co.kr.ticketing.adminconcert.place.usecase.AddPlaceUseCase;
import co.kr.ticketing.adminconcert.place.usecase.GetPlaceUseCase;
import jakarta.validation.Valid;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;

@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/places")
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
public class PlaceController {
AddPlaceUseCase addPlaceUseCase;
GetPlaceUseCase getPlaceUseCase;

@GetMapping
public ResponseEntity<ResponseDto<GetPlaceResponse>> get(@Valid @ModelAttribute GetPlaceRequest request) {
var response = getPlaceUseCase.execute(request);
return ResponseEntity.ok(new ResponseDto<>(PlaceResponseCode.GET_PLACE.name(), response));
}

@PostMapping
public ResponseEntity<ResponseDto<CreatedDto>> addPlace(@RequestBody @Valid AddPlaceRequest request) {
long createdId = addPlaceUseCase.execute(request);

return ResponseEntity.ok(new ResponseDto<>(PlaceResponseCode.CREATED_PLACE.name(), CreatedDto.from(createdId)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package co.kr.ticketing.adminconcert.place.controller.request;

import java.util.List;

import co.kr.ticketing.adminconcert.place.domain.model.Place;
import co.kr.ticketing.adminconcert.place.domain.model.Seat;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;

public record AddPlaceRequest(
@NotBlank
String name,
@NotBlank
String address,
@Valid
@NotEmpty
List<SeatRequest> seats,
String identifier
) {
public record SeatRequest(
@NotNull
Integer columnNum,
@NotNull
Integer rowNum,
@NotNull
Integer floor
) {
public Seat toModel() {
return Seat.builder()
.columnNum(columnNum)
.rowNum(rowNum)
.floor(floor)
.build();
}
}

public Place toModel() {
return Place.builder()
.name(name)
.address(address)
.identifier(identifier)
.last(true)
.seats(seats.stream().map(SeatRequest::toModel).toList())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package co.kr.ticketing.adminconcert.place.controller.request;

import jakarta.validation.constraints.NotBlank;

public record GetPlaceRequest(
@NotBlank
String name
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package co.kr.ticketing.adminconcert.place.controller.response;

import java.util.List;

import co.kr.ticketing.adminconcert.place.domain.model.Place;
import lombok.Builder;

@Builder
public record GetPlaceResponse(
List<PlaceResponse> places
) {
@Builder
public record PlaceResponse(
Long id,
String identifier,
String name
) {
public static PlaceResponse from(Place place) {
return PlaceResponse.builder()
.id(place.id())
.identifier(place.identifier())
.name(place.name())
.build();
}
}

public static GetPlaceResponse from(List<Place> places) {
return GetPlaceResponse.builder()
.places(places.stream().map(PlaceResponse::from).toList())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package co.kr.ticketing.adminconcert.place.controller.response;

public enum PlaceResponseCode {
GET_PLACE,
CREATED_PLACE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package co.kr.ticketing.adminconcert.place.domain;

public interface IdentifierGenerator {
String generate();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package co.kr.ticketing.adminconcert.place.domain.infrastructure;

import java.util.List;
import java.util.Optional;

import co.kr.ticketing.adminconcert.place.domain.infrastructure.dto.UpdatePlaceDto;
import co.kr.ticketing.adminconcert.place.domain.model.Place;

public interface PlaceRepository {
Optional<Place> find(long id);

Optional<Place> findLastVersion(String identifier);

List<Place> getLastVersionsByPlaceName(String placeName);

Place create(Place place);

Place update(long id, UpdatePlaceDto updateDto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package co.kr.ticketing.adminconcert.place.domain.infrastructure.dto;

import lombok.Builder;

@Builder
public record UpdatePlaceDto(
String name,
String address,
String identifier,
Long version,
boolean last
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package co.kr.ticketing.adminconcert.place.domain.model;

import java.util.List;

import co.kr.ticketing.adminconcert.place.domain.IdentifierGenerator;
import lombok.Builder;

@Builder
public record Place(
Long id,
String name,
String address,
String identifier,
Long version,
boolean last,
List<Seat> seats
) {
public static Place getNewPlace(Place place, IdentifierGenerator generator) {
return Place.builder()
.name(place.name())
.address(place.address())
.identifier(generator.generate())
.version(0L)
.last(true)
.seats(List.copyOf(place.seats()))
.build();
}

public Place getNextPlace(Place beforePlace) {
return Place.builder()
.name(name)
.address(address)
.identifier(identifier)
.version(beforePlace.getNextVersion())
.last(true)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get 에서 last 를 셋팅하는 write operation 이 같이 있는데요. 분리하면 좋을 것 같습니다.
함수는 getNextPlace 로 DB 변경이 없을 것이 기대되지만 실재로는 beforePlace 의 last 가 Update 되는 함수라 안정적이지 않아보입니다.
차라리 last 를 별도 루틴에서 셋팅하게해서 read / write 를 분리하는게 좋을것 같습니다.

.seats(List.copyOf(seats()))
.build();
}

public Long getNextVersion() {
return this.version + 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package co.kr.ticketing.adminconcert.place.domain.model;

import lombok.Builder;

@Builder
public record Seat(
Long id,
int columnNum,
int rowNum,
int floor
) {
}
Loading
Loading