Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/#18 get program detail #20

Merged
merged 4 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.sopt.sopkerton.common.exception.ProgramSuccess;
import org.sopt.sopkerton.program.domain.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;
Expand All @@ -29,4 +27,16 @@ public ResponseEntity<ApiResponse<List<ProgramListResponse>>> programListView(@R
ApiResponse.success(ProgramSuccess.PROGRAM_LIST_VIEW_SUCCESS, programListByProgramType)
);
}

@GetMapping("/detail")
public ResponseEntity<ApiResponse> orderProgramDetail(
@RequestParam("programId") Long programId
) {
Object programDetail = programService.getProgramDetail(1L, programId);
return ResponseEntity
.status(ProgramSuccess.PROGRAM_DETAIL_VIEW_SUCCESS.getHttpStatus())
.body(
ApiResponse.success(ProgramSuccess.PROGRAM_DETAIL_VIEW_SUCCESS, programDetail)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.sopt.sopkerton.program.domain.exception;

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

@AllArgsConstructor
public enum ProgramError implements ErrorBase {
PROGRAM_NOT_FOUND(HttpStatus.NOT_FOUND, "Can not found Program."),

;

private final HttpStatus status;
private final String errorMessage;
@Override
public int getHttpStatusCode() {
return this.status.value();
}

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

@Override
public String getErrorMessage() {
return this.errorMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.sopt.sopkerton.program.domain.exception;

import org.sopt.sopkerton.common.exception.base.ExceptionBase;

public class ProgramException extends ExceptionBase {
public ProgramException(ProgramError errorBase) {
super(errorBase);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.sopt.sopkerton.common.exception;
package org.sopt.sopkerton.program.domain.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.")
PROGRAM_LIST_VIEW_SUCCESS(HttpStatus.OK, "Get Program List View Data Successful."),
PROGRAM_DETAIL_VIEW_SUCCESS(HttpStatus.OK, "Get Program Detail View Data Successful.")
;

private final HttpStatus status;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.sopt.sopkerton.program.dto.response;

import com.fasterxml.jackson.annotation.JsonProperty;

public abstract class ProgramDetailResponse {

public record VolunteerDetail(
@JsonProperty("imageUrl")
String imageUrl,
@JsonProperty("content")
String content,
@JsonProperty("organizationName")
String organizationName,
@JsonProperty("registerAt")
String registerAt,
@JsonProperty("hour")
int hour,
@JsonProperty("isApply")
boolean isApply
) {
}

public record EmploymentDetail(
@JsonProperty("imageUrl")
String imageUrl,
@JsonProperty("content")
String content,
@JsonProperty("organizationName")
String organizationName,
@JsonProperty("registerAt")
String registerAt,
@JsonProperty("salary")
int salary,
@JsonProperty("isApply")
boolean isApply
) {
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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;
Expand All @@ -10,4 +11,5 @@ 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
Expand Up @@ -6,14 +6,24 @@
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.sopt.sopkerton.program.domain.Program;
import org.sopt.sopkerton.program.domain.exception.ProgramError;
import org.sopt.sopkerton.program.domain.exception.ProgramException;
import org.sopt.sopkerton.program.dto.response.ProgramDetailResponse;
import org.sopt.sopkerton.program.dto.response.ProgramListResponse;
import org.sopt.sopkerton.program.infrastructure.ProgramRepository;
import org.sopt.sopkerton.user.domain.Apply;
import org.sopt.sopkerton.user.domain.enums.ApplyStatus;
import org.sopt.sopkerton.user.infrastructure.ApplyRepository;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ProgramService {
private static final String VOLUNTEER_TYPE = "VOLUNTEERING";
private static final String EMPLOYMENT_TYPE = "EMPLOYMENT";

private final ProgramRepository programRepository;
private final ApplyRepository applyRepository;

public List<ProgramListResponse> getProgramListByProgramType(String programType) {
List<Program> programs = programRepository.findAllByProgramType(programType);
Expand All @@ -31,10 +41,40 @@ public List<ProgramListResponse> getProgramListByProgramType(String programType)
return programListResponses;
}

public Object getProgramDetail(Long userId, Long programId) {
Program program = programRepository.findById(programId)
.orElseThrow(() -> new ProgramException(ProgramError.PROGRAM_NOT_FOUND));
Apply apply = applyRepository.findByUserIdAndProgramId(userId, programId).get();
boolean isApply = convertToIsApply(apply.getIsApply());
if (program.getType().equals(VOLUNTEER_TYPE)) {
return new ProgramDetailResponse.VolunteerDetail(
program.getImageUrl(),
program.getContent(),
program.getOrganizationName(),
formatToLocalDate(program.getRegisterAt()),
program.getVolunteerHours(),
isApply
);
}
if (program.getType().equals(EMPLOYMENT_TYPE)){
return new ProgramDetailResponse.EmploymentDetail(
program.getImageUrl(),
program.getContent(),
program.getOrganizationName(),
formatToLocalDate(program.getRegisterAt()),
program.getSalary(),
isApply
);
}
return null;
}

private String formatToLocalDate(LocalDateTime localDateTime) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM์›” dd์ผ");
return localDateTime.format(formatter);
}


private boolean convertToIsApply(ApplyStatus status) {
return status.equals(ApplyStatus.APPLY);
}
}
29 changes: 29 additions & 0 deletions src/main/java/org/sopt/sopkerton/user/domain/Apply.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.sopt.sopkerton.user.domain;

import jakarta.persistence.*;
import lombok.*;
import org.sopt.sopkerton.common.domain.BaseEntity;
import org.sopt.sopkerton.user.domain.enums.ApplyStatus;

@Getter
@Entity
@Table(schema = "skt-t1-app", name = "applies")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
public class Apply extends BaseEntity {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;

@Column(name = "user_id", nullable = false)
Long userId;

@Column(name = "program_id", nullable = false)
Long programId;

@Column(name = "is_apply", nullable = false)
@Enumerated(value = EnumType.STRING)
ApplyStatus isApply;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.sopt.sopkerton.user.domain.enums;

public enum ApplyStatus {

APPLY, UNAPPLY

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

import org.sopt.sopkerton.user.domain.Apply;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface ApplyRepository extends JpaRepository<Apply, Long> {

Optional<Apply> findByUserIdAndProgramId(Long userId, Long programId);
}