Skip to content

Commit

Permalink
Merge pull request #88 from team-Ollie/develop
Browse files Browse the repository at this point in the history
[develop] Tag 관련 enum 정의 및 프로그램 목록 조회 구현
  • Loading branch information
JoongHyun-Kim authored Jul 1, 2024
2 parents 269df5e + 47b50eb commit a3f308f
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public enum BaseResponseStatus {
INVALID_CHALLENGE_IDX(false, HttpStatus.NOT_FOUND, "잘못된 ChallengeIdx 입니다."),
NO_CHALLENGE(false, HttpStatus.NOT_FOUND, "Challenge 가 존재하지 않습니다."),

// tag
WRONG_TAG_NAME(false, HttpStatus.BAD_REQUEST, "잘못된 태그 종류입니다."),
WRONG_TAG_DETAIL(false, HttpStatus.BAD_REQUEST, "잘못된 태그 이름입니다."),

/**
* Response 오류
*/
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/ollie/wecare/common/enums/TagEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ollie.wecare.common.enums;

import lombok.Getter;
import ollie.wecare.common.base.BaseException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import static ollie.wecare.common.base.BaseResponseStatus.WRONG_TAG_DETAIL;
import static ollie.wecare.common.base.BaseResponseStatus.WRONG_TAG_NAME;

@Getter
public enum TagEnum {
LOCATION("위치", null),
SEOUL("서울", LOCATION),
GYEONGGI("경기", LOCATION),
OTHER("그 외", LOCATION),
CATEGORY("카테고리", null),
SPORTS("운동", CATEGORY),
ART("예술", CATEGORY),
ACADEMIC("학술", CATEGORY),
ETC("기타", CATEGORY);

private final String tagName;
private final TagEnum parent;
private final List<TagEnum> child;

TagEnum(String name, TagEnum parent) {
this.child = new ArrayList<>();
this.tagName = name;
this.parent = parent;
if(Objects.nonNull(parent)) {
parent.child.add(this);
}
}

public static TagEnum getEnumByName(String name) throws BaseException {
return Arrays.stream(TagEnum.values())
.filter(tagEnum -> tagEnum.getTagName().equalsIgnoreCase(name))
.findFirst()
.orElseThrow(() -> new BaseException(WRONG_TAG_NAME));
}

public static TagEnum getTag(TagEnum tagEnum, String tagDetail) throws BaseException {
return Arrays.stream(TagEnum.values())
.filter(t -> t.parent == tagEnum && t.getTagName().equalsIgnoreCase(tagDetail))
.findFirst()
.orElseThrow(() -> new BaseException(WRONG_TAG_DETAIL));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ollie.wecare.program.dto.GetProgramDetailRes;
import ollie.wecare.program.dto.GetProgramRes;
import ollie.wecare.program.dto.PostProgramReq;
import ollie.wecare.program.dto.ProgramListResponse;
import ollie.wecare.program.service.ProgramService;
import org.springframework.web.bind.annotation.*;

Expand All @@ -20,28 +21,26 @@
public class ProgramController {
private final ProgramService programService;

/*
* 프로그램 조회 (월별)
* */

// 프로그램 조회 (월별)
@GetMapping
@ResponseBody
public BaseResponse<List<GetProgramRes>> getPrograms(@RequestParam(value = "year", defaultValue = "0", required = false) Long year,
@RequestParam(value = "month", defaultValue = "0", required = false) Long month) {
public BaseResponse<List<GetProgramRes>> getPrograms(@RequestParam(value = "year", defaultValue = "0", required = false) Long year, @RequestParam(value = "month", defaultValue = "0", required = false) Long month) {
return new BaseResponse<>(programService.getPrograms(year, month));
}

// 프로그램 전체 목록 조회
@GetMapping("/list")
public BaseResponse<List<ProgramListResponse>> getProgramList() {
return programService.getProgramList();
}

// 프로그램 상세 조회
@GetMapping("/{programIdx}")
@ResponseBody
public BaseResponse<GetProgramDetailRes> getProgram(@PathVariable(value = "programIdx") Long programIdx) {
return new BaseResponse<>(programService.getProgram(programIdx));
}

/*
* 프로그램 등록
* */
// 프로그램 등록
@PostMapping
@ResponseBody
public BaseResponse<String> saveProgram(@RequestBody PostProgramReq postProgramReq) throws BaseException {
programService.saveProgram(postProgramReq);
return new BaseResponse<>(SUCCESS);
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/ollie/wecare/program/dto/ProgramListResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ollie.wecare.program.dto;

import ollie.wecare.common.enums.TagEnum;
import ollie.wecare.program.entity.Program;
import ollie.wecare.program.entity.Tag;

import java.time.LocalDateTime;

public record ProgramListResponse(Long programIdx,
String name,
DateDto openDate,
DateDto dueDate,
String location,
String category) {
private static DateDto convertToDateDto(LocalDateTime dueDate) {
if (dueDate == null) return null;
return new DateDto(
dueDate.getYear(),
dueDate.getMonthValue(),
dueDate.getDayOfMonth());
}

public static ProgramListResponse fromProgram(Program program) {
return new ProgramListResponse(
program.getProgramIdx(),
program.getName(),
convertToDateDto(program.getOpenDate()),
convertToDateDto(program.getDueDate()),
program.getLocation(),
getCategoryTag(program) != null ? getCategoryTag(program).getTagName() : null);
}

private static TagEnum getCategoryTag(Program program) {
return program.getTags().stream()
.map(Tag::getName)
.filter(tagEnum -> tagEnum.getParent() == TagEnum.CATEGORY)
.findFirst()
.orElse(null);
}
}
11 changes: 7 additions & 4 deletions src/main/java/ollie/wecare/program/entity/Program.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package ollie.wecare.program.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;
import ollie.wecare.common.base.BaseEntity;
import org.hibernate.annotations.DynamicInsert;

Expand Down Expand Up @@ -41,4 +38,10 @@ public class Program extends BaseEntity {
@OneToMany(fetch = FetchType.LAZY)
private List<Tag> tags;

public void setTags(List<Tag> tags) {
this.tags = tags;
for (Tag tag : this.tags) {
tag.setProgram(this);
}
}
}
18 changes: 10 additions & 8 deletions src/main/java/ollie/wecare/program/entity/Tag.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
package ollie.wecare.program.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;
import ollie.wecare.common.base.BaseEntity;
import ollie.wecare.common.enums.TagEnum;
import org.hibernate.annotations.DynamicInsert;

@Entity
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@DynamicInsert
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Tag extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "tag_idx")
private Long tagIdx;

private String name;
@Enumerated(EnumType.STRING)
private TagEnum name;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "program_idx")
private Program program;

public void setProgram(Program program) {
this.program = program;
if (program != null) { program.getTags().add(this); }
}
}
12 changes: 12 additions & 0 deletions src/main/java/ollie/wecare/program/service/ProgramService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import ollie.wecare.challenge.entity.Challenge;
import ollie.wecare.challenge.repository.ChallengeRepository;
import ollie.wecare.common.base.BaseException;
import ollie.wecare.common.base.BaseResponse;
import ollie.wecare.common.enums.Role;
import ollie.wecare.program.dto.GetProgramDetailRes;
import ollie.wecare.program.dto.GetProgramRes;
import ollie.wecare.program.dto.PostProgramReq;
import ollie.wecare.program.dto.ProgramListResponse;
import ollie.wecare.program.entity.Program;
import ollie.wecare.program.repository.ProgramRepository;
import ollie.wecare.user.entity.User;
Expand All @@ -33,6 +35,7 @@ public class ProgramService {

private final UserRepository userRepository;

// 프로그램 월별 조회
public List<GetProgramRes> getPrograms(Long year, Long month) {
int y = year != null && year > 0 ? year.intValue() : LocalDateTime.now().getYear();
int m = month != null && month > 0 ? month.intValue() : LocalDateTime.now().getMonthValue();
Expand All @@ -45,6 +48,15 @@ public List<GetProgramRes> getPrograms(Long year, Long month) {
.toList();
}

// 프로그램 전체 목록 조회
public BaseResponse<List<ProgramListResponse>> getProgramList() {
List<ProgramListResponse> programList = programRepository.findAll().stream()
.map(ProgramListResponse::fromProgram)
.toList();
return new BaseResponse<>(programList);
}

// 프로그램 상세 조회
public GetProgramDetailRes getProgram(Long programIdx) {
return GetProgramDetailRes.fromProgram(
programRepository.findById(programIdx).orElseThrow(()-> new BaseException(INVALID_PROGRAM_IDX)));
Expand Down

0 comments on commit a3f308f

Please sign in to comment.