Skip to content

Commit

Permalink
Merge pull request #11 from Kusitms-28th-Meet-Up-B/feature/#1/mainAPI
Browse files Browse the repository at this point in the history
feat : main API 추가
  • Loading branch information
MyunghyunNero authored Oct 28, 2023
2 parents 98c44e6 + 2e41319 commit b7f8af5
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 6 deletions.
17 changes: 17 additions & 0 deletions src/main/java/kusitms/gallae/config/BaseException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package kusitms.gallae.config;

public class BaseException extends RuntimeException {
private final BaseResponseStatus status;

public BaseException(BaseResponseStatus status) {
this.status = status;
}

public BaseResponseStatus getStatus() {
return this.status;
}

public String getMessage() {
return this.status.getMessage();
}
}
18 changes: 18 additions & 0 deletions src/main/java/kusitms/gallae/config/ExceptionHandlerAdvice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package kusitms.gallae.config;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class ExceptionHandlerAdvice {

@ExceptionHandler(BaseException.class)
public ResponseEntity<BaseResponse<?>> handleBaseException(BaseException e, HttpServletRequest request) {

return ResponseEntity.status(e.getStatus().getHttpStatus()).body(new BaseResponse<>(e.getStatus()));
}
}
8 changes: 4 additions & 4 deletions src/main/java/kusitms/gallae/config/OpenAPIConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class OpenAPIConfig {
private final String prodUrl;

public OpenAPIConfig(
@Value("${votogether.openapi.dev-url}") final String devUrl,
@Value("${votogether.openapi.prod-url}") final String prodUrl
@Value("${travelmaker.openapi.dev-url}") final String devUrl,
@Value("${travelmaker.openapi.prod-url}") final String prodUrl
) {
this.devUrl = devUrl;
this.prodUrl = prodUrl;
Expand All @@ -36,9 +36,9 @@ public OpenAPI openAPI() {
prodServer.description("운영 환경 서버 URL");

final Info info = new Info()
.title("VoTogether API")
.title("TravelMaker API")
.version("v1.0.0")
.description("보투게더 API");
.description("TravelMaker API");

return new OpenAPI()
.info(info)
Expand Down
55 changes: 54 additions & 1 deletion src/main/java/kusitms/gallae/controller/ProgramController.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,79 @@
package kusitms.gallae.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Positive;
import kusitms.gallae.config.BaseResponse;
import kusitms.gallae.dto.program.ProgramDetailRes;
import kusitms.gallae.dto.program.ProgramMainRes;
import kusitms.gallae.service.ProgramService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/program")
@RequestMapping("/programs")
public class ProgramController {

private final ProgramService programService;

@Operation(summary = "최신 프로그램 상위 4개", description = """
최근에 등록된 프로그램 상위 4개를 반환 합니다.
""")
@GetMapping("/recent")
public ResponseEntity<BaseResponse<List<ProgramMainRes>>> findRecentProgram(){
return ResponseEntity.ok(new BaseResponse<>(this.programService.getRecentPrograms()));
}

@Operation(summary = "프로그램 유형별 프로그램들", description = """
여행지원사업, 여행공모전, 여행대외활동 세가지 유형별로 프로그램들을 반환합니다.
""")
@GetMapping("/type")
public ResponseEntity<BaseResponse<List<ProgramMainRes>>> findProgramsByProgramType(
@Parameter(description = "프로그램 유형", example = "여행지원사업, 여행공모전, 여행대외활동")
@RequestParam(value = "programType", required = true)
String programType,

@Parameter(description = "페이지 번호")
@Positive(message = "must be greater than 0")
@RequestParam(value = "page", defaultValue = "1")
Integer pageNumber,

@Parameter(description = "페이징 사이즈 (최대 100)")
@Min(value = 1, message = "must be greater than or equal to 1")
@Max(value = 100, message = "must be less than or equal to 100")
@RequestParam(value = "size", defaultValue = "20")
Integer pagingSize
) {

PageRequest pageRequest = PageRequest.of(pageNumber,pagingSize);
return ResponseEntity.ok(new BaseResponse<>(this.programService.getProgramsByProgramType(programType,pageRequest)));
}

@Operation(summary = "인기 많은 프로그램들", description = """
찜수가 가장 높은 프로그램들을 상위 4개를 반환합니다.
""")
@GetMapping("/best")
public ResponseEntity<BaseResponse<List<ProgramMainRes>>> findBestPrograms(){
return ResponseEntity.ok(new BaseResponse<>(this.programService.getBestPrograms()));
}

@GetMapping("/program")
public ResponseEntity<BaseResponse<ProgramDetailRes>> findProgramDetail(
@Parameter(description = "프로그램 ID")
@RequestParam(value = "id", required = false) Long id
){
return ResponseEntity.ok(new BaseResponse<>(this.programService.getProgramDetail(id)));
}

}
2 changes: 2 additions & 0 deletions src/main/java/kusitms/gallae/domain/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class Program {

private String location;

private String description;

private LocalDateTime recruitStartDate;

private LocalDateTime recruitEndDate;
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/kusitms/gallae/dto/program/ProgramDetailRes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package kusitms.gallae.dto.program;


import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.time.LocalDateTime;

@Data
public class ProgramDetailRes {

private Long id;

private String photoUrl;

private String programName;

private Long Like;

private String remainDay;

private String location;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul")
private LocalDateTime recruitStartDate;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul")
private LocalDateTime recruitEndDate;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul")
private LocalDateTime tripStartDate;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul")
private LocalDateTime tripEndDate;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul")
private LocalDateTime activeStartDate;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul")
private LocalDateTime activeEndDate;

private String contact;

private String contactNumber;

private String programLink;

private String description;
}
2 changes: 2 additions & 0 deletions src/main/java/kusitms/gallae/dto/program/ProgramMainRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
@Data
public class ProgramMainRes {

private Long id;

private String photoUrl;

private String programName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kusitms.gallae.repository;

import kusitms.gallae.domain.Program;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
Expand All @@ -10,7 +12,9 @@ public interface ProgramRespository extends JpaRepository<Program, Long> {
@Override
Optional<Program> findById(Long programId);

List<Program> findByProgramType(String programType);
Page<Program> findAllByProgramTypeOrderByCreatedAtDesc(String programType , Pageable pageable);

List<Program> findTop4ByOrderByCreatedAtDesc();

List<Program> findTop4ByOrderByProgramLikeDesc();
}
9 changes: 9 additions & 0 deletions src/main/java/kusitms/gallae/service/ProgramService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package kusitms.gallae.service;

import kusitms.gallae.dto.program.ProgramDetailRes;
import kusitms.gallae.dto.program.ProgramMainRes;
import org.springframework.data.domain.Pageable;

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

public interface ProgramService {

ProgramDetailRes getProgramDetail(Long id);

List<ProgramMainRes> getRecentPrograms();

List<ProgramMainRes> getProgramsByProgramType(String programType , Pageable pageable);

List<ProgramMainRes> getBestPrograms();
}
48 changes: 48 additions & 0 deletions src/main/java/kusitms/gallae/service/ProgramServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@


import jakarta.transaction.Transactional;
import kusitms.gallae.config.BaseException;
import kusitms.gallae.config.BaseResponseStatus;
import kusitms.gallae.dto.program.ProgramDetailRes;
import kusitms.gallae.global.DurationCalcurator;
import kusitms.gallae.domain.Program;
import kusitms.gallae.dto.program.ProgramMainRes;
import kusitms.gallae.repository.ProgramRespository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

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

Expand All @@ -22,8 +28,50 @@ public class ProgramServiceImpl implements ProgramService {
public List<ProgramMainRes> getRecentPrograms(){
List<Program> programs = programRespository.findTop4ByOrderByCreatedAtDesc();

return getProgramMainRes(programs);
}

@Override
public List<ProgramMainRes> getProgramsByProgramType(String programType, Pageable pageable) {
Page<Program> programs = programRespository.findAllByProgramTypeOrderByCreatedAtDesc(programType , pageable);
List<Program> pageToListNewPrograms = programs.getContent();
return getProgramMainRes(pageToListNewPrograms);
}

@Override
public List<ProgramMainRes> getBestPrograms(){
List<Program> programs = programRespository.findTop4ByOrderByProgramLikeDesc();
return getProgramMainRes(programs);
}

@Override
public ProgramDetailRes getProgramDetail(Long id){
Program program = programRespository.findById(id).orElse(null);
if(program == null) {
throw new BaseException(BaseResponseStatus.BAD_REQUEST);
}else{
ProgramDetailRes programDetailRes = new ProgramDetailRes();
programDetailRes.setId(program.getId());
programDetailRes.setProgramName(program.getProgramName());
programDetailRes.setProgramLink(program.getProgramLink());
programDetailRes.setContact(program.getContact());
programDetailRes.setContactNumber(program.getContactNumber());
programDetailRes.setDescription(program.getDescription());
programDetailRes.setLocation(program.getLocation());
programDetailRes.setActiveStartDate(program.getActiveStartDate());
programDetailRes.setActiveEndDate(program.getActiveEndDate());
programDetailRes.setRecruitStartDate(program.getRecruitStartDate());
programDetailRes.setRecruitEndDate(program.getRecruitEndDate());
programDetailRes.setTripStartDate(program.getTripStartDate());
programDetailRes.setTripEndDate(program.getTripEndDate());
return programDetailRes;
}
}

private List<ProgramMainRes> getProgramMainRes(List<Program> programs){
return programs.stream().map(program -> {
ProgramMainRes programMainRes = new ProgramMainRes();
programMainRes.setId(program.getId());
programMainRes.setProgramName(program.getProgramName());
programMainRes.setLike(program.getProgramLike());
programMainRes.setPhotoUrl(program.getPhotoUrl());
Expand Down

0 comments on commit b7f8af5

Please sign in to comment.