Skip to content

Commit

Permalink
feat : 생협 운영시간 id로 조회 로직 수정 (#1164)
Browse files Browse the repository at this point in the history
* feat : 생협 운영시간 이름으로 조회 추가

* feat: coopName 추가

* chore : 여백 추가

* chore : 여백 추가

* chore : 테스트 코드 수정

* fix: 대즐 운영시간 표기 오류 수정
  • Loading branch information
Choon0414 authored Feb 6, 2025
1 parent 969304e commit ff44654
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public record InnerCoopShop(
public static InnerCoopShop from(CoopShop coopShop) {
return new InnerCoopShop(
coopShop.getId(),
coopShop.getName(),
coopShop.getCoopName().getName(),
coopShop.getCoopOpens().stream()
.map(InnerCoopOpens::from)
.toList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public interface CoopShopApi {
@ApiResponse(responseCode = "403", content = @Content(schema = @Schema(hidden = true)))
}
)
@Operation(summary = "특정 생협 매장 정보 조회")
@GetMapping("/{coopShopId}")
@Operation(summary = "특정 생협 매장 정보 고유 번호로 조회")
@GetMapping("/{coopNameId}")
ResponseEntity<CoopShopResponse> getCoopShop(
@Parameter(in = PATH) @PathVariable("coopShopId") Integer coopShopId
@Parameter(in = PATH) @PathVariable("coopNameId") Integer coopNameId
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public ResponseEntity<CoopShopsResponse> getCoopShops() {
return ResponseEntity.ok(coopShops);
}

@GetMapping("/{coopShopId}")
@GetMapping("/{coopNameId}")
public ResponseEntity<CoopShopResponse> getCoopShop(
@PathVariable Integer coopShopId
@PathVariable Integer coopNameId
) {
CoopShopResponse coopShop = coopShopService.getCoopShop(coopShopId);
CoopShopResponse coopShop = coopShopService.getCoopShop(coopNameId);
return ResponseEntity.ok(coopShop);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public record CoopShopResponse(
public static CoopShopResponse from(CoopShop coopShop) {
return new CoopShopResponse(
coopShop.getId(),
coopShop.getName(),
coopShop.getCoopName().getName(),
coopShop.getCoopSemester().getSemester(),
coopShop.getCoopOpens().stream()
.map(InnerCoopOpens::from)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public record InnerCoopShop(
public static InnerCoopShop from(CoopShop coopShop) {
return new InnerCoopShop(
coopShop.getId(),
coopShop.getName(),
coopShop.getCoopName().getName(),
coopShop.getCoopOpens().stream()
.map(InnerCoopOpens::from)
.toList(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package in.koreatech.koin.domain.coopshop.model;

import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

import in.koreatech.koin.global.domain.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@Table(name = "coop_names")
@NoArgsConstructor(access = PROTECTED)
public class CoopName {

@Id
@GeneratedValue(strategy = IDENTITY)
private Integer id;

@NotNull
@Column(name = "name", nullable = false)
private String name;

@Builder
private CoopName(
String name
) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public class CoopShop extends BaseEntity {
@JoinColumn(name = "semester_id", referencedColumnName = "id", nullable = false)
private CoopSemester coopSemester;

@NotNull
@Column(name = "name", nullable = false)
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "coop_name_id", referencedColumnName = "id", nullable = false)
private CoopName coopName;

@NotNull
@Column(name = "phone", nullable = false)
Expand All @@ -58,13 +58,13 @@ public class CoopShop extends BaseEntity {
@Builder
private CoopShop(
CoopSemester coopSemester,
String name,
CoopName coopName,
String phone,
String location,
String remarks
) {
this.coopSemester = coopSemester;
this.name = name;
this.coopName = coopName;
this.phone = phone;
this.location = location;
this.remarks = remarks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
@Getter
public enum CoopShopType {
CAFETERIA("학생식당"),
WELFARE_CENTER("복지관"),
WELFARE_CENTER("복지관식당"),
CAMPUS2("2캠퍼스"),
WELFARE_CVS("복지관 편의점"),
CHAMBIT_CVS("참빛관 편의점"),
CVS("복지관 참빛관 편의점"),
BOOK_STORE("서점"),
DAZZLE("대즐"),
LAUNDRY("세탁소"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package in.koreatech.koin.domain.coopshop.repository;

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

import org.springframework.data.repository.Repository;

import in.koreatech.koin.domain.coopshop.exception.CoopShopNotFoundException;
import in.koreatech.koin.domain.coopshop.model.CoopName;
import in.koreatech.koin.domain.coopshop.model.CoopShopType;

public interface CoopNameRepository extends Repository<CoopName, Integer> {

CoopName save(CoopName coopNames);

List<CoopName> findAll();

Optional<CoopName> findById(Integer id);

Optional<CoopName> findByName(String coopName);

default CoopName getById(Integer id) {
return findById(id)
.orElseThrow(() -> CoopShopNotFoundException.withDetail("coopShopId : " + id));
}

default CoopName getByName(CoopShopType coopShopType) {
return findByName(coopShopType.getCoopShopName())
.orElseThrow(() -> CoopShopNotFoundException.withDetail("coopShopType : " + coopShopType));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import org.springframework.data.repository.Repository;

import in.koreatech.koin.domain.coopshop.exception.CoopShopNotFoundException;
import in.koreatech.koin.domain.coopshop.model.CoopSemester;
import in.koreatech.koin.domain.coopshop.model.CoopShop;
import in.koreatech.koin.domain.coopshop.model.CoopShopType;

public interface CoopShopRepository extends Repository<CoopShop, Integer> {

Expand All @@ -17,16 +17,16 @@ public interface CoopShopRepository extends Repository<CoopShop, Integer> {

Optional<CoopShop> findById(Integer id);

Optional<CoopShop> findByNameAndCoopSemesterId(String name, Integer semesterId);
Optional<CoopShop> findByCoopNameIdAndCoopSemester(Integer coopNameId, CoopSemester semester);

default CoopShop getById(Integer id) {
return findById(id)
.orElseThrow(() -> CoopShopNotFoundException.withDetail("coopShopId : " + id));
}

default CoopShop getByNameAndCoopSemesterId(CoopShopType name, Integer semesterId) {
return findByNameAndCoopSemesterId(name.getCoopShopName(), semesterId)
default CoopShop getByCoopNameIdAndCoopSemester(Integer coopNameId, CoopSemester semester) {
return findByCoopNameIdAndCoopSemester(coopNameId, semester)
.orElseThrow(() -> CoopShopNotFoundException
.withDetail("coopShopName : " + name.getCoopShopName() + ", semesterId: " + semesterId));
.withDetail(String.format("coopNameId : %d, semesterId: %d", coopNameId, semester.getId())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
import in.koreatech.koin.domain.coopshop.dto.CoopShopsResponse;
import in.koreatech.koin.domain.coopshop.exception.CoopSemesterNotFoundException;
import in.koreatech.koin.domain.coopshop.exception.DiningTypeNotFoundException;
import in.koreatech.koin.domain.coopshop.model.CoopName;
import in.koreatech.koin.domain.coopshop.model.CoopOpen;
import in.koreatech.koin.domain.coopshop.model.CoopSemester;
import in.koreatech.koin.domain.coopshop.model.CoopShop;
import in.koreatech.koin.domain.coopshop.model.CoopShopType;
import in.koreatech.koin.domain.coopshop.model.DayType;
import in.koreatech.koin.domain.coopshop.repository.CoopNameRepository;
import in.koreatech.koin.domain.coopshop.repository.CoopOpenRepository;
import in.koreatech.koin.domain.coopshop.repository.CoopSemesterRepository;
import in.koreatech.koin.domain.coopshop.repository.CoopShopRepository;
Expand All @@ -37,14 +39,16 @@ public class CoopShopService {
private final CoopShopRepository coopShopRepository;
private final CoopOpenRepository coopOpenRepository;
private final CoopSemesterRepository coopSemesterRepository;
private final CoopNameRepository coopNameRepository;

public CoopShopsResponse getCoopShops() {
CoopSemester coopSemester = coopSemesterRepository.getByIsApplied(true);
return CoopShopsResponse.from(coopSemester);
}

public CoopShopResponse getCoopShop(Integer id) {
CoopShop coopShop = coopShopRepository.getById(id);
public CoopShopResponse getCoopShop(Integer coopNameId) {
CoopSemester currentSemester = coopSemesterRepository.getByIsApplied(true);
CoopShop coopShop = coopShopRepository.getByCoopNameIdAndCoopSemester(coopNameId, currentSemester);
return CoopShopResponse.from(coopShop);
}

Expand All @@ -53,8 +57,9 @@ public boolean getIsOpened(LocalDateTime now, CoopShopType coopShopType, DiningT
DayType todayType =
(now.getDayOfWeek() == DayOfWeek.SATURDAY || now.getDayOfWeek() == DayOfWeek.SUNDAY)
? DayType.WEEKEND : DayType.WEEKDAYS;
CoopSemester semester = coopSemesterRepository.getByIsApplied(true);
CoopShop coopShop = coopShopRepository.getByNameAndCoopSemesterId(coopShopType, semester.getId());
CoopSemester currentSemester = coopSemesterRepository.getByIsApplied(true);
CoopName coopName = coopNameRepository.getByName(coopShopType);
CoopShop coopShop = coopShopRepository.getByCoopNameIdAndCoopSemester(coopName.getId(), currentSemester);
CoopOpen open = coopOpenRepository
.getByCoopShopAndTypeAndDayOfWeek(coopShop, type.getDiningName(), todayType);

Expand All @@ -74,17 +79,17 @@ public boolean getIsOpened(LocalDateTime now, CoopShopType coopShopType, DiningT
}
}

public DiningType getDiningType(){
if(LocalTime.now(clock).isAfter(BREAKFAST.getStartTime().minusHours(1))
&& LocalTime.now(clock).isBefore(BREAKFAST.getEndTime())){
public DiningType getDiningType() {
if (LocalTime.now(clock).isAfter(BREAKFAST.getStartTime().minusHours(1))
&& LocalTime.now(clock).isBefore(BREAKFAST.getEndTime())) {
return BREAKFAST;
}
if(LocalTime.now(clock).isAfter(LUNCH.getStartTime().minusHours(1))
&& LocalTime.now(clock).isBefore(LUNCH.getEndTime())){
if (LocalTime.now(clock).isAfter(LUNCH.getStartTime().minusHours(1))
&& LocalTime.now(clock).isBefore(LUNCH.getEndTime())) {
return LUNCH;
}
if(LocalTime.now(clock).isAfter(DINNER.getStartTime().minusHours(1))
&& LocalTime.now(clock).isBefore(DINNER.getEndTime())){
if (LocalTime.now(clock).isAfter(DINNER.getStartTime().minusHours(1))
&& LocalTime.now(clock).isBefore(DINNER.getEndTime())) {
return DINNER;
}

Expand Down
18 changes: 18 additions & 0 deletions src/main/resources/db/migration/V111__add_table_coop_names.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE TABLE `coop_names`
(
`id` INT UNSIGNED AUTO_INCREMENT NOT NULL comment '고유 id' primary key,
`name` VARCHAR(255) NOT NULL comment '생협 운영장 이름'
);

INSERT INTO `coop_names` (`name`)
VALUES ('학생식당'),
('복지관식당'),
('대즐'),
('서점'),
('세탁소'),
('복사실'),
('복지관 참빛관 편의점'),
('미용실'),
('오락실'),
('우편취급국'),
('안경원');
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ALTER TABLE `coop_shop`
ADD COLUMN `coop_name_id` INT UNSIGNED comment '생협 운영장 고유 id',
ADD CONSTRAINT `coop_name_fk_id` FOREIGN KEY (`coop_name_id`) REFERENCES `coop_names` (`id`) ON DELETE CASCADE;

UPDATE `coop_shop` AS cs
JOIN `coop_names` AS cn
ON cs.`name` = cn.`name`
SET cs.`coop_name_id` = cn.`id`;

ALTER TABLE `coop_shop`
DROP COLUMN `name`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
UPDATE coop_opens AS co
JOIN coop_shop AS cs ON co.coop_shop_id = cs.id
JOIN coop_names AS cn ON cs.coop_name_id = cn.id
SET co.day_of_week = 'WEEKDAYS'
WHERE cn.name = '대즐' AND co.open_time = '08:30';
Loading

0 comments on commit ff44654

Please sign in to comment.