Skip to content

Commit

Permalink
Merge pull request #17 from SOPT-33-iOS-Team-1/feat/#13-separate_prog…
Browse files Browse the repository at this point in the history
…ram_type_api

Feat/#13 separate program type api
  • Loading branch information
yummygyudon authored Nov 25, 2023
2 parents 19fff23 + fcb5d5d commit bae531b
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 9 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
@@ -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)
);
}
}
10 changes: 6 additions & 4 deletions src/main/java/org/sopt/sopkerton/program/domain/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@
@Getter
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn
@DiscriminatorColumn(name = "program_type")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "programs")
public abstract class Program extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "program_id")
private Long id;

@Column(nullable = false)
private String title;

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

@Column(nullable = false)
private String organizationName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

@Getter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public enum Category {
VOLUNTEERING("봉사"),
EMPLOYMENT("취업");

private final String value;
public enum Status {
REGISTER, DONE;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.sopt.sopkerton.program.dto.request;

import com.fasterxml.jackson.annotation.JsonProperty;

public record ProgramListRequest(
@JsonProperty("program_type")
String programType
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.sopt.sopkerton.program.dto.response;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public record ProgramListResponse(
long programId,
String title,
String registerAt,
String imageUrl,
String Region
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.sopt.sopkerton.program.infrastructure;

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> {

@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
@@ -0,0 +1,39 @@
package org.sopt.sopkerton.program.service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.sopt.sopkerton.program.domain.Program;
import org.sopt.sopkerton.program.dto.response.ProgramListResponse;
import org.sopt.sopkerton.program.infrastructure.ProgramRepository;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ProgramService {
private final ProgramRepository programRepository;

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

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일");
return localDateTime.format(formatter);
}


}

0 comments on commit bae531b

Please sign in to comment.