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

[BE] 세미나실, 시간표 CRUD 설계 #169

Merged
merged 16 commits into from
Nov 24, 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
import java.util.ArrayList;
import java.util.List;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.example.backend.reservation.domain.Reservation;
import org.example.backend.seminarRoom.domain.dto.SeminarRoomReqDto;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "seminar_room")
public class SeminarRoom {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "seminar_room_id")
Expand All @@ -40,4 +41,28 @@ public class SeminarRoom {

@OneToMany(mappedBy = "seminarRoom", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Reservation> reservations = new ArrayList<>();
}

@Builder
public SeminarRoom(String name, Integer personCapacity, String place, String image) {
this.name = name;
this.personCapacity = personCapacity;
this.place = place;
this.image = image;
}

public static SeminarRoom of(SeminarRoomReqDto dto) {
return SeminarRoom.builder()
.name(dto.getName())
.personCapacity(dto.getPersonCapacity())
.place(dto.getPlace())
.image(dto.getImage())
.build();
}

public void update(SeminarRoomReqDto dto) {
this.name = dto.getName();
this.personCapacity = dto.getPersonCapacity();
this.place = dto.getPlace();
this.image = dto.getImage();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.example.backend.seminarRoom.domain.controller;

import lombok.RequiredArgsConstructor;
import org.example.backend.seminarRoom.domain.dto.SeminarRoomReqDto;
import org.example.backend.seminarRoom.domain.dto.SeminarRoomResDto;
import org.example.backend.seminarRoom.service.SeminarRoomService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/seminar-rooms")
public class SeminarRoomController {
private final SeminarRoomService seminarRoomService;

@PostMapping
public ResponseEntity<SeminarRoomResDto> createSeminarRoom(@RequestBody SeminarRoomReqDto reqDto) {
SeminarRoomResDto resDto = seminarRoomService.createSeminarRoom(reqDto);
return ResponseEntity.ok(resDto);
}

@GetMapping("/{id}")
public ResponseEntity<SeminarRoomResDto> getSeminarRoom(@PathVariable Long id) {
SeminarRoomResDto resDto = seminarRoomService.getSeminarRoom(id);
return ResponseEntity.ok(resDto);
}

@PutMapping("/{id}")
public ResponseEntity<SeminarRoomResDto> updateSeminarRoom(
@PathVariable Long id,
@RequestBody SeminarRoomReqDto reqDto) {
SeminarRoomResDto resDto = seminarRoomService.updateSeminarRoom(id, reqDto);
return ResponseEntity.ok(resDto);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteSeminarRoom(@PathVariable Long id) {
seminarRoomService.deleteSeminarRoom(id);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.example.backend.seminarRoom.domain.dto;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class SeminarRoomReqDto {
private String name;
private Integer personCapacity;
private String place;
private String image;

@Builder
private SeminarRoomReqDto(String name, Integer personCapacity, String place, String image) {
this.name = name;
this.personCapacity = personCapacity;
this.place = place;
this.image = image;
}

public static SeminarRoomReqDto of(String name, Integer personCapacity, String place, String image) {
return SeminarRoomReqDto.builder()
.name(name)
.personCapacity(personCapacity)
.place(place)
.image(image)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.example.backend.seminarRoom.domain.dto;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.example.backend.seminarRoom.domain.SeminarRoom;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class SeminarRoomResDto {
private Long id;
private String name;
private Integer personCapacity;
private String place;
private String image;

@Builder
private SeminarRoomResDto(Long id, String name, Integer personCapacity, String place, String image) {
this.id = id;
this.name = name;
this.personCapacity = personCapacity;
this.place = place;
this.image = image;
}

public static SeminarRoomResDto of(SeminarRoom seminarRoom) {
return SeminarRoomResDto.builder()
.id(seminarRoom.getId())
.name(seminarRoom.getName())
.personCapacity(seminarRoom.getPersonCapacity())
.place(seminarRoom.getPlace())
.image(seminarRoom.getImage())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.example.backend.seminarRoom.exception;

import lombok.RequiredArgsConstructor;
import org.example.backend.common.exception.BaseException;
import org.example.backend.common.exception.BaseExceptionType;
import org.example.backend.seminar.exception.SeminarExceptionType;

@RequiredArgsConstructor
public class SeminarRoomException extends BaseException {
private final SeminarRoomExceptionType exceptionType;
@Override
public BaseExceptionType exceptionType() {
return exceptionType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.example.backend.seminarRoom.exception;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.NOT_FOUND;

import lombok.RequiredArgsConstructor;
import org.example.backend.common.exception.BaseExceptionType;
import org.springframework.http.HttpStatus;

@RequiredArgsConstructor
public enum SeminarRoomExceptionType implements BaseExceptionType {
NOT_FOUND_SEMINAR_ROOM(NOT_FOUND, "세미나룸를 찾을 수 없습니다"),
INVALID_NAME_VALUE(BAD_REQUEST, "세미나실 이름은 필수입니다."),
INVALID_CAPACITY(BAD_REQUEST, "수용 인원은 1명 이상이어야 합니다.");
;

private final HttpStatus httpStatus;
private final String errorMessage;

@Override
public HttpStatus httpStatus() {
return null;
}

@Override
public String errorMessage() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example.backend.seminarRoom.repository;

import org.example.backend.seminarRoom.domain.SeminarRoom;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SeminarRoomRepository extends JpaRepository<SeminarRoom, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.example.backend.seminarRoom.service;

import static org.example.backend.seminarRoom.exception.SeminarRoomExceptionType.INVALID_CAPACITY;
import static org.example.backend.seminarRoom.exception.SeminarRoomExceptionType.INVALID_NAME_VALUE;
import static org.example.backend.seminarRoom.exception.SeminarRoomExceptionType.NOT_FOUND_SEMINAR_ROOM;

import lombok.RequiredArgsConstructor;
import org.example.backend.seminarRoom.domain.SeminarRoom;
import org.example.backend.seminarRoom.domain.dto.SeminarRoomReqDto;
import org.example.backend.seminarRoom.domain.dto.SeminarRoomResDto;
import org.example.backend.seminarRoom.exception.SeminarRoomException;
import org.example.backend.seminarRoom.repository.SeminarRoomRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class SeminarRoomService {
private final SeminarRoomRepository seminarRoomRepository;

@Transactional
public SeminarRoomResDto createSeminarRoom(SeminarRoomReqDto reqDto) {
validateSeminarRoom(reqDto);

SeminarRoom seminarRoom = SeminarRoom.of(reqDto);
seminarRoomRepository.save(seminarRoom);

return SeminarRoomResDto.of(seminarRoom);
}

public SeminarRoomResDto getSeminarRoom(Long id) {
SeminarRoom seminarRoom = seminarRoomRepository.findById(id)
.orElseThrow(() -> new SeminarRoomException(NOT_FOUND_SEMINAR_ROOM));
return SeminarRoomResDto.of(seminarRoom);
}

@Transactional
public SeminarRoomResDto updateSeminarRoom(Long id, SeminarRoomReqDto reqDto) {
validateSeminarRoom(reqDto);

SeminarRoom seminarRoom = seminarRoomRepository.findById(id)
.orElseThrow(() -> new SeminarRoomException(NOT_FOUND_SEMINAR_ROOM));

seminarRoom.update(reqDto);
return SeminarRoomResDto.of(seminarRoom);
}

@Transactional
public void deleteSeminarRoom(Long id) {
SeminarRoom seminarRoom = seminarRoomRepository.findById(id)
.orElseThrow(() -> new SeminarRoomException(NOT_FOUND_SEMINAR_ROOM));
seminarRoomRepository.delete(seminarRoom);
}

private void validateSeminarRoom(SeminarRoomReqDto reqDto) {
if (reqDto.getName() == null || reqDto.getName().trim().isEmpty()) {
throw new SeminarRoomException(INVALID_NAME_VALUE);
}
if (reqDto.getPersonCapacity() == null || reqDto.getPersonCapacity() <= 0) {
throw new SeminarRoomException(INVALID_CAPACITY);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.example.backend.timetable.controller;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.example.backend.timetable.domain.dto.TimetableReqDto;
import org.example.backend.timetable.domain.dto.TimetableResDto;
import org.example.backend.timetable.service.TimetableService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
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
@RequiredArgsConstructor
@RequestMapping("/api/timetables")
public class TimetableController {
private final TimetableService timetableService;

@PostMapping
public ResponseEntity<TimetableResDto> createTimetable(@RequestBody TimetableReqDto reqDto) {
TimetableResDto resDto = timetableService.createTimetable(reqDto);
return ResponseEntity.ok(resDto);
}
@GetMapping
public ResponseEntity<List<TimetableResDto>> getAllTimetables() {
List<TimetableResDto> timetables = timetableService.getAllTimetables();
return ResponseEntity.ok(timetables);
}

@PutMapping("/{timetableId}")
public ResponseEntity<TimetableResDto> updateTimetable(
@PathVariable Long timetableId,
@RequestBody TimetableReqDto reqDto) {
TimetableResDto resDto = timetableService.updateTimetable(timetableId, reqDto);
return ResponseEntity.ok(resDto);
}

@DeleteMapping("/{timetableId}")
public ResponseEntity<Void> deleteTimetable(@PathVariable Long timetableId) {
timetableService.deleteTimetable(timetableId);
return ResponseEntity.ok().build();
}
}
Loading