Skip to content

Commit

Permalink
Merge pull request #18 from KUSITMS-Team-A/feature/15-find-store
Browse files Browse the repository at this point in the history
[Feature] 가게 찾기 API 구현
  • Loading branch information
yeop0740 authored Nov 19, 2023
2 parents b0936ea + a55912d commit 90e74cf
Show file tree
Hide file tree
Showing 34 changed files with 1,128 additions and 2 deletions.
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = 'backend'
rootProject.name = 'Backend'
2 changes: 2 additions & 0 deletions src/main/java/com/backend/BackendApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

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

@EnableJpaAuditing
@SpringBootApplication
public class BackendApplication {

Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/backend/common/domain/BaseEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.backend.common.domain;

import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import lombok.ToString;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

import java.time.LocalDateTime;

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@Getter
public abstract class BaseEntity {

@CreatedDate
private LocalDateTime createdTime;

@LastModifiedDate
private LocalDateTime modifiedTime;

}
12 changes: 12 additions & 0 deletions src/main/java/com/backend/common/dto/ResponseDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,24 @@ public static <T> ResponseEntity<T> ok(T data) {
return ResponseEntity.ok(data);
}

public static ResponseEntity<Void> ok() {
return ResponseEntity
.status(HttpStatus.CREATED)
.build();
}

public static <T> ResponseEntity<T> created(T data) {
return ResponseEntity
.status(HttpStatus.CREATED)
.body(data);
}

public static ResponseEntity<Void> created() {
return ResponseEntity
.status(HttpStatus.CREATED)
.build();
}

public static ResponseEntity<Void> noContent() {
return ResponseEntity
.status(HttpStatus.NO_CONTENT)
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/backend/config/SwaggerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,28 @@
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import org.springdoc.core.utils.SpringDocUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static org.springdoc.core.utils.Constants.DEFAULT_SERVER_DESCRIPTION;

@Configuration
public class SwaggerConfig {

@Value("${swagger.request-server}")
private String SERVER_BASE_URL;

@Bean
public OpenAPI openAPI() {
return new OpenAPI()
.servers(List.of(new Server().url(SERVER_BASE_URL).description(DEFAULT_SERVER_DESCRIPTION)))
.components(new Components()
.addSecuritySchemes("bearer-key",
new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT")))
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/backend/domain/benefit/entity/Benefit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.backend.domain.benefit.entity;

import com.backend.common.domain.BaseEntity;
import com.backend.domain.contract.entity.Contract;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Table(name = "benefit")
public class Benefit extends BaseEntity {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long benefitId;

@Enumerated(EnumType.STRING)
private BenefitType type;

private int amount;

private String content;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "contract_id")
private Contract contract;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.backend.domain.benefit.entity;

public enum BenefitType {

FIX, RATE, MENU

}
42 changes: 42 additions & 0 deletions src/main/java/com/backend/domain/contract/entity/Contract.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.backend.domain.contract.entity;

import com.backend.domain.benefit.entity.Benefit;
import com.backend.common.domain.BaseEntity;
import com.backend.domain.store.entity.Store;
import com.backend.domain.university.entity.University;
import jakarta.persistence.*;
import lombok.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Entity
@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Table(name = "contract")
public class Contract extends BaseEntity {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long contractId;

private LocalDate startDate;

private LocalDate endDate;

private String manager;

@OneToMany(mappedBy = "contract", cascade = CascadeType.PERSIST)
private List<Benefit> benefits = new ArrayList<>();

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "university_id")
private University university;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "store_id")
private Store store;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.backend.domain.contract.entity;

public enum ContractStatus {
AVAILABLE, EXPIRATION
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.backend.domain.store.controller;

import com.backend.common.dto.ResponseDto;
import com.backend.domain.auth.dto.Login;
import com.backend.domain.auth.dto.LoginUser;
import com.backend.domain.store.dto.*;
import com.backend.domain.store.service.StoreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/store")
@SecurityRequirement(name = "bearer-key")
@Tag(name = "가게", description = "가게 관련 API")
public class StoreController {

private final StoreService storeService;

@PostMapping
public ResponseEntity<Void> createStore(@RequestBody CreateStoreRequest request) {
storeService.createStore(request);
return ResponseDto.created();
}

@GetMapping("/details/{storeId}")
@Operation(
summary = "가게 상세 정보 조회",
responses = {
@ApiResponse(
responseCode = "200",
content = @Content(schema = @Schema(implementation = ReadStoreDetailsDto.class)))
})
public ResponseEntity<ReadStoreDetailsDto> readStoreDetails(@Parameter(hidden = true) @Login LoginUser loginUser, @Parameter(name = "storeId") @PathVariable Long storeId) {
return ResponseDto.ok(storeService.readStoreDetails(loginUser, storeId));
}

@GetMapping("/search")
@Operation(
summary = "가게 목록 검색 및 조회",
description =
"<p>{url}/store/search?isPicked=true&name=건&category=FOOD&pageSize=40&pageNumber=0</p>" +
"<p>isPicked = true/false, default = false</p>" +
"<p>name = String 타입의 가게 명, 입력하지 않아도 사용 가능</p>" +
"<p>category = [FOOD, CAFE, BEAUTY, CULTURE, ETC, NONE] 중 하나, default = NONE(카테고리 검색X)</p>" +
"<p>pageSize = int 값, default = 40</p>" +
"<p>pageNumber = 0부터 int 값, default = 0</p>",
responses = {
@ApiResponse(
responseCode = "200",
content = @Content(schema = @Schema(implementation = ReadStoresDto.class)))
})
public ResponseEntity<ReadStoresDto> readStores(@Parameter(hidden = true) @Login LoginUser loginUser, @ModelAttribute ReadStoresRequest request) {
return ResponseDto.ok(storeService.readStores(loginUser, request));
}

@PostMapping("/pick")
@Operation(
summary = "픽 등록",
responses = {
@ApiResponse(responseCode = "201")
})
public ResponseEntity<Void> createPick(@Parameter(hidden = true) @Login LoginUser loginUser, @RequestBody PickRequest request) {
storeService.createPick(loginUser, request);
return ResponseDto.created();
}

@DeleteMapping("/pick")
@Operation(
summary = "픽 취소",
responses = {
@ApiResponse(responseCode = "200")
})
public ResponseEntity<Void> deletePick(@Parameter(hidden = true) @Login LoginUser loginUser, @RequestBody PickRequest request) {
storeService.deletePick(loginUser, request);
return ResponseDto.ok();
}

}
29 changes: 29 additions & 0 deletions src/main/java/com/backend/domain/store/dto/BusinessHourDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.backend.domain.store.dto;

import com.backend.domain.store.entity.BusinessHour;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class BusinessHourDto {

private String dayOfWeek;

private String openingTime;

private String closingTime;

public static BusinessHourDto from(BusinessHour businessHour) {
return BusinessHourDto.builder()
.dayOfWeek(businessHour.getDayOfWeek().getDayOfWeek())
.openingTime(businessHour.getOpeningTime())
.closingTime(businessHour.getClosingTime())
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.backend.domain.store.dto;

import com.backend.domain.store.entity.BusinessDay;
import com.backend.domain.store.entity.BusinessHour;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CreateBusinessHourDto {

private String dayOfWeek;

private HoursInfoDto hoursInfo;

public BusinessHour toEntity() {
return BusinessHour.builder()
.dayOfWeek(BusinessDay.convert(dayOfWeek))
.openingTime(hoursInfo.getOpeningTime())
.closingTime(hoursInfo.getClosingTime())
.build();
}

}
68 changes: 68 additions & 0 deletions src/main/java/com/backend/domain/store/dto/CreateStoreDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.backend.domain.store.dto;

import com.backend.domain.store.entity.Category;
import com.backend.domain.store.entity.Store;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CreateStoreDto {

private String storeName;
private String genre;
private String phoneNumber;
private String address;
private LocationDto location;
private List<CreateBusinessHourDto> businessHours = new ArrayList<>();
private List<MenuDto> menu = new ArrayList<>();

public Store toEntity(Category category) {
return Store.builder()
.name(storeName)
.address(address)
.description(getDescription())
.latitude(getLatitude())
.longitude(getLongitude())
.category(category)
.contact(phoneNumber)
.phoneNumber(phoneNumber)
.businessHours(
businessHours.stream()
.filter(Objects::nonNull)
.map(CreateBusinessHourDto::toEntity)
.toList()
)
.build();
}

public String getDescription() {
if (genre != null) {
return genre;
}
if (!menu.isEmpty()) {
return menu.stream()
.map(MenuDto::getName)
.limit(3)
.collect(Collectors.joining(", "));
}
return null;
}


public Double getLongitude() {
return location == null ? null : location.getLongitude();
}

public Double getLatitude() {
return location == null ? null : location.getLatitude();
}

}
Loading

0 comments on commit 90e74cf

Please sign in to comment.