Skip to content

Commit

Permalink
git commit -m "feat/#13 program List 반환 api 개발"
Browse files Browse the repository at this point in the history
  • Loading branch information
kseysh committed Nov 25, 2023
1 parent c740730 commit 013eb7c
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.sopt.sopkerton.common.exception;

public enum ProgramError {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.sopt.sopkerton.common.exception;

import lombok.AllArgsConstructor;
import org.sopt.sopkerton.common.exception.base.SuccessBase;
import org.springframework.http.HttpStatus;

@AllArgsConstructor
public enum ProgramSuccess implements SuccessBase {
PROGRAM_LIST_VIEW_SUCCESS(HttpStatus.OK, "Get Program List View Data Successful.")
;

private final HttpStatus status;
private final String successMessage;

@Override
public int getHttpStatusCode() {
return this.status.value();
}

@Override
public HttpStatus getHttpStatus() {
return this.status;
}

@Override
public String getSuccessMessage() {
return this.successMessage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.sopt.sopkerton.common.exception.base.ErrorBase;
import org.sopt.sopkerton.common.exception.base.SuccessBase;

@Getter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ApiResponse<T> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.sopt.sopkerton.program.controller;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.sopt.sopkerton.common.exception.ProgramSuccess;
import org.sopt.sopkerton.common.response.ApiResponse;
import org.sopt.sopkerton.program.dto.request.ProgramListRequest;
import org.sopt.sopkerton.program.dto.response.ProgramListResponse;
import org.sopt.sopkerton.program.service.ProgramService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/program")
public class ProgramController {
private final ProgramService programService;

@GetMapping("")
public ResponseEntity<ApiResponse<List<ProgramListResponse>>> programListView(@RequestParam(name = "program_type") String type) {
List<ProgramListResponse> programListByProgramType = programService.getProgramListByProgramType(type);
return ResponseEntity
.status(ProgramSuccess.PROGRAM_LIST_VIEW_SUCCESS.getHttpStatus())
.body(
ApiResponse.success(ProgramSuccess.PROGRAM_LIST_VIEW_SUCCESS, programListByProgramType)
);
}
}
5 changes: 2 additions & 3 deletions src/main/java/org/sopt/sopkerton/program/domain/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ public abstract class Program extends BaseEntity {
@Column(nullable = false)
private String title;

@Enumerated(EnumType.STRING)
@Column(nullable = false)
private Category category;
private String imageUrl;
private String type;

@Column(nullable = false)
private String organizationName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public record ProgramListResponse(
long programId,
String title,
String registerAt,
String imageUrl,
String Region
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import java.util.List;
import org.sopt.sopkerton.program.domain.Program;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface ProgramRepository extends JpaRepository<Program, Long> {
List<Program> findByProgramType(String programType);

@Query("select p from Program p where p.type = :type order by p.registerAt")
List<Program> findAllByProgramType(@Param("type")String programType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ public class ProgramService {
private final ProgramRepository programRepository;

public List<ProgramListResponse> getProgramListByProgramType(String programType) {
List<Program> programs = programRepository.findByProgramType(programType);
List<Program> programs = programRepository.findAllByProgramType(programType);

// Program 엔티티를 ProgramListView로 변환
List<ProgramListResponse> programListResponses = programs.stream()
.map(program -> new ProgramListResponse(
program.getId(),
program.getTitle(),
formatToLocalDate(program.getRegisterAt()),
program.getImageUrl(),
program.getRegion()
))
.collect(Collectors.toList());
return programListResponses;
}

private String formatToLocalDate(LocalDateTime localDateTime) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM.dd");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM월 dd일");
return localDateTime.format(formatter);
}

Expand Down

0 comments on commit 013eb7c

Please sign in to comment.