Skip to content

Commit

Permalink
refactor: schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
BAEKDODAM committed Feb 26, 2024
1 parent b9b2d28 commit 99c39d2
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.ImageTrip.Schedule.controller;

import com.ImageTrip.Response.MultiResponseDto;
import com.ImageTrip.Response.PageInfo;
import com.ImageTrip.Schedule.dto.ScheduleDto;
import com.ImageTrip.Schedule.entity.Schedule;
import com.ImageTrip.Schedule.mapper.ScheduleMapper;
import com.ImageTrip.Schedule.service.ScheduleService;
import com.ImageTrip.ScheduleList.dto.ScheduleListDto;
import com.ImageTrip.ScheduleList.entity.ScheduleList;
import com.ImageTrip.member.entity.Member;
import com.ImageTrip.member.service.MemberService;
import io.swagger.annotations.ApiOperation;
Expand All @@ -27,6 +24,7 @@ public class ScheduleController {
private final ScheduleMapper mapper;
private final MemberService memberService;
private static final int PAGE_DEFAULT_SIZE = 10;
private static final long DEFAULT_MEMBER_ID = 0;

public ScheduleController(ScheduleService scheduleService, ScheduleMapper mapper, MemberService memberService) {
this.scheduleService = scheduleService;
Expand All @@ -37,7 +35,7 @@ public ScheduleController(ScheduleService scheduleService, ScheduleMapper mapper
@PostMapping
@ApiOperation(value = "일정 생성")
public ResponseEntity postSchedule(@Valid @RequestBody ScheduleDto.Post requestBody,
@RequestHeader(value = "Authorization") String token) throws Exception, IOException {
@RequestHeader(value = "Authorization") String token) {
Member member = memberService.findMemberByToken(token);
List<ScheduleListDto.Post> scheduleList = requestBody.getScheduleList();
Schedule postSchedule = mapper.schedulePostDtoToSchedule(requestBody);
Expand All @@ -60,29 +58,26 @@ public ResponseEntity patchSchedule(@PathVariable("scheduleId") int scheduleId,
@ApiOperation(value = "일정 삭제")
@DeleteMapping("/{scheduleId}")
public ResponseEntity deleteSchedule(@PathVariable("scheduleId") int scheduleId,
@RequestHeader(value = "Authorization") String token){
@RequestHeader(value = "Authorization") String token){
long memberId = memberService.getMemberIdFromToken(token);
scheduleService.deleteSchedule(memberId, scheduleId);
return new ResponseEntity<>(HttpStatus.OK);
}

@ApiOperation(value = "내가 작성한 일정 목록 조회")
@GetMapping("/my")
public ResponseEntity getMySchedules(/*final Pageable pageablePageSize,*/ long cursor,
@RequestHeader(value = "Authorization") String token){
Member member = memberService.findMemberByToken(token);
List<ScheduleDto.ListResponse> mySchedules = scheduleService.findMyScheduleByPage(cursor, member, PageRequest.of(0, PAGE_DEFAULT_SIZE));
public ResponseEntity getMySchedules(@RequestParam long cursor,
@RequestHeader(value = "Authorization") String token){
long memberId = memberService.getMemberIdFromToken(token);
List<ScheduleDto.ListResponse> mySchedules = scheduleService.findMyScheduleByPage(cursor, memberId, PageRequest.of(0, PAGE_DEFAULT_SIZE));
return new ResponseEntity<>(mySchedules, HttpStatus.OK);
}

@ApiOperation(value = "모든 공유 일정 목록 조회")
@GetMapping
public ResponseEntity getAllSchedules(long cursor,
public ResponseEntity getAllSchedules(@RequestParam long cursor,
@RequestHeader(value = "Authorization", required = false) String token){
long memberId = 0;
if(token != null){
memberId = memberService.getMemberIdFromToken(token);
}
long memberId = token != null? memberService.getMemberIdFromToken(token):DEFAULT_MEMBER_ID;
List<ScheduleDto.ListResponse> allSchedules = scheduleService.findSharedSchedulesByPage(cursor, PageRequest.of(0, PAGE_DEFAULT_SIZE), memberId);
return new ResponseEntity<>(allSchedules, HttpStatus.OK);
}
Expand All @@ -91,10 +86,7 @@ public ResponseEntity getAllSchedules(long cursor,
@GetMapping("/{scheduleId}")
public ResponseEntity getSchedule(@PathVariable("scheduleId") long scheduleId,
@RequestHeader(value = "Authorization", required = false) String token) {
long memberId = 0;
if(token != null){
memberId = memberService.getMemberIdFromToken(token);
}
long memberId = token != null? memberService.getMemberIdFromToken(token):DEFAULT_MEMBER_ID;
ScheduleDto.Response response = scheduleService.getScheduleDetail(scheduleId, memberId);
return new ResponseEntity<>(response, HttpStatus.OK);
}
Expand Down
26 changes: 7 additions & 19 deletions src/main/java/com/ImageTrip/Schedule/dto/ScheduleDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.ImageTrip.Schedule.entity.Schedule;
import com.ImageTrip.ScheduleList.dto.ScheduleListDto;
import com.ImageTrip.ScheduleList.entity.ScheduleList;
import com.ImageTrip.member.entity.Member;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

Expand Down Expand Up @@ -42,13 +41,15 @@ public static class Response {

private LocalDate endDate;

private int likeCnt;
@Builder.Default
private int likeCnt = 0;

private String name;

private List<ScheduleList> scheduleLists;
private boolean share;
private boolean liked;
@Builder.Default
private boolean liked = false;

public static Response from(Schedule schedule, List<ScheduleList> scheduleLists, int likeCnt, boolean liked){
return Response.builder()
Expand All @@ -64,15 +65,14 @@ public static Response from(Schedule schedule, List<ScheduleList> scheduleLists,
.build();
}

public static Response from(Schedule schedule, Member member, List<ScheduleList> scheduleLists, int likeCnt) {
public static Response from(Schedule schedule, List<ScheduleList> scheduleLists) {
return Response.builder()
.scheduleId(schedule.getScheduleId())
.title(schedule.getTitle())
.scheduleLists(schedule.getScheduleLists())
.scheduleLists(scheduleLists)
.startDate(schedule.getStartDate())
.endDate(schedule.getEndDate())
.likeCnt(likeCnt)
.name(member.getName())
.name(schedule.getMember().getName())
.share(schedule.getShare())
.build();
}
Expand Down Expand Up @@ -108,17 +108,5 @@ public static ListResponse from (Schedule schedule, int likeCnt, boolean liked){
.name(schedule.getMember().getName())
.build();
}

public static ListResponse from (Schedule schedule, Member member, int likeCnt, boolean liked){
return ListResponse.builder()
.scheduleId(schedule.getScheduleId())
.title(schedule.getTitle())
.startDate(schedule.getStartDate())
.endDate(schedule.getEndDate())
.likeCnt(likeCnt)
.liked(liked)
.name(member.getName())
.build();
}
}
}
9 changes: 8 additions & 1 deletion src/main/java/com/ImageTrip/Schedule/entity/Schedule.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.springframework.data.annotation.LastModifiedDate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
Expand Down Expand Up @@ -49,4 +48,12 @@ public class Schedule {
@ManyToOne
@JoinColumn(name = "memberId")
private Member member;

public Schedule(String title, Boolean share, LocalDate startDate, LocalDate endDate, Member member) {
this.title = title;
this.share = share;
this.startDate = startDate;
this.endDate = endDate;
this.member = member;
}
}
51 changes: 23 additions & 28 deletions src/main/java/com/ImageTrip/Schedule/service/ScheduleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ public ScheduleDto.Response createSchedule(Schedule postSchedule, List<ScheduleL
postSchedule.setMember(member);
Schedule saveSchedule = scheduleRepository.save(postSchedule);
List<ScheduleList> SaveScheduleLists = scheduleListService.saveScheduleLists(scheduleLists, saveSchedule);
Schedule findSchedule = findVerifiedSchedule(saveSchedule.getScheduleId());
ScheduleDto.Response response = ScheduleDto.Response.from(findSchedule, member, SaveScheduleLists, 0);

ScheduleDto.Response response = ScheduleDto.Response.from(saveSchedule, SaveScheduleLists);
return response;
}

Expand All @@ -61,8 +59,7 @@ public ScheduleDto.Response updateSchedule(long memberId, long scheduleId, Sched
.ifPresent(schedules -> scheduleListService.changeScheduleLists(scheduleId, schedules, findSchedule));

int likeCnt = likeService.scheduleLikeCnt(scheduleId);
boolean liked = likeService.findLike(scheduleId, memberId);

boolean liked = likeService.hasLiked(scheduleId, memberId);
List<ScheduleList> returnScheduleList = scheduleListService.findSchduleListByScheduleId(scheduleId);

return ScheduleDto.Response.from(saveSchedule, returnScheduleList, likeCnt, liked);
Expand All @@ -76,32 +73,26 @@ public void deleteSchedule(long memberId, long scheduleId){
scheduleRepository.deleteById(scheduleId);
}

public List<ScheduleDto.ListResponse> findMyScheduleByPage(long cursor, Member member, Pageable pageable){
//Member member = memberService.getMemberByMemberId(memberId)
List<Schedule> schedules = getMySchedules(cursor, pageable, member.getMemberId())
public List<ScheduleDto.ListResponse> findMyScheduleByPage(long cursor, long memberId, Pageable pageable){
List<Schedule> schedules = getMySchedules(cursor, pageable, memberId)
.stream().collect(Collectors.toList());

return schedules.stream().map(schedule -> {
int likeCnt = likeService.scheduleLikeCnt(schedule.getScheduleId());
boolean liked = likeService.findLike(schedule.getScheduleId(), member.getMemberId());
return ScheduleDto.ListResponse.from(schedule, member, likeCnt, liked);
}).collect(Collectors.toList());
return schedules.stream()
.map(schedule -> makeListResponse(schedule, memberId))
.collect(Collectors.toList());
}
// 페이징 처리를 위한 메서드
public List<Schedule> getMySchedules(Long cursor, Pageable page, long memberId){
public List<Schedule> getMySchedules(Long cursor, Pageable pageable, long memberId){
return cursor.equals(0L)
? scheduleRepository.findByMemberMemberIdOrderByScheduleIdDesc(memberId, page)
: scheduleRepository.findByMemberMemberIdAndScheduleIdLessThanOrderByScheduleIdDesc(memberId,cursor, page);
? scheduleRepository.findByMemberMemberIdOrderByScheduleIdDesc(memberId, pageable)
: scheduleRepository.findByMemberMemberIdAndScheduleIdLessThanOrderByScheduleIdDesc(memberId, cursor, pageable);
}

public List<ScheduleDto.ListResponse> findSharedSchedulesByPage(long cursor, Pageable pageable, long memberId){
List<Schedule> schedules = getSharedSchedules(cursor, pageable)
.stream().collect(Collectors.toList());
return schedules.stream().map(schedule -> {
int likeCnt = likeService.scheduleLikeCnt(schedule.getScheduleId());
boolean liked = likeService.findLike(schedule.getScheduleId(), memberId);
return ScheduleDto.ListResponse.from(schedule, likeCnt, liked);
}).collect(Collectors.toList());
return schedules.stream()
.map(schedule -> makeListResponse(schedule, memberId))
.collect(Collectors.toList());
}

public List<Schedule> getSharedSchedules(Long cursor, Pageable pageable){
Expand All @@ -114,19 +105,17 @@ public List<Schedule> getSharedSchedules(Long cursor, Pageable pageable){
public ScheduleDto.Response getScheduleDetail(long scheduleId, long memberId) {
Schedule findSchedule = findVerifiedSchedule(scheduleId);
int likeCnt = likeService.scheduleLikeCnt(scheduleId);
boolean liked = likeService.findLike(scheduleId, memberId);
boolean liked = likeService.hasLiked(scheduleId, memberId);
List<ScheduleList> scheduleLists = scheduleListService.findSchduleListByScheduleId(scheduleId);
return ScheduleDto.Response.from(findSchedule, scheduleLists, likeCnt, liked);
}

public List<ScheduleDto.ListResponse> findSearchSchedule(Long cursor, Pageable pageable, String search, long memberId) {
List<Schedule> schedules = getSearchSchedule(cursor, pageable, search)
.stream().collect(Collectors.toList());
return schedules.stream().map(schedule -> {
int likeCnt = likeService.scheduleLikeCnt(schedule.getScheduleId());
boolean liked = likeService.findLike(schedule.getScheduleId(), memberId);
return ScheduleDto.ListResponse.from(schedule, likeCnt, liked);
}).collect(Collectors.toList());
return schedules.stream()
.map(schedule -> makeListResponse(schedule, memberId))
.collect(Collectors.toList());

}
public List<Schedule> getSearchSchedule(Long cursor, Pageable pageable, String search) {
Expand All @@ -141,4 +130,10 @@ public void validateWriter(long memberId, Schedule schedule) {
public Schedule findVerifiedSchedule(long scheduleId) {
return scheduleRepository.findByScheduleId(scheduleId).orElseThrow(() -> new BusinessLogicException(ExceptionCode.SCHEDULE_NOT_FOUND));
}

public ScheduleDto.ListResponse makeListResponse(Schedule schedule, long memberId){
int likeCnt = likeService.scheduleLikeCnt(schedule.getScheduleId());
boolean liked = likeService.hasLiked(schedule.getScheduleId(), memberId);
return ScheduleDto.ListResponse.from(schedule, likeCnt, liked);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import lombok.Getter;
import lombok.Setter;

import javax.persistence.Column;

@Getter
public class ScheduleListDto {
@Getter
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/ImageTrip/ScheduleList/entity/ScheduleList.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import com.ImageTrip.Schedule.entity.Schedule;
import com.fasterxml.jackson.annotation.JsonBackReference;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;

@Entity
@Getter
@Setter
@NoArgsConstructor
public class ScheduleList {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -25,4 +27,12 @@ public class ScheduleList {
@JsonBackReference
@JoinColumn(name = "scheduleId")
private Schedule schedule;

public ScheduleList(String content, float lat, float lon, int priority, Schedule schedule) {
this.content = content;
this.lat = lat;
this.lon = lon;
this.priority = priority;
this.schedule = schedule;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ public ScheduleListService(ScheduleListRepository repository) {
public List<ScheduleList> saveScheduleLists(List<ScheduleListDto.Post> schedules, Schedule schedule) {
return schedules.stream().map(scheduleList -> {
ScheduleList scheduleList1 = new ScheduleList(scheduleList.getContent(), scheduleList.getLat(), scheduleList.getLon(),schedules.indexOf(scheduleList), schedule);
/*scheduleList1.setContent(scheduleList.getContent());
scheduleList1.setLat(scheduleList.getLat());
scheduleList1.setLon(scheduleList.getLon());
int priority = schedules.indexOf(scheduleList);
scheduleList1.setPriority(priority);
scheduleList1.setSchedule(schedule);*/
return repository.save(scheduleList1);
}).collect(Collectors.toList());
}
Expand All @@ -40,9 +34,9 @@ public List<ScheduleList> changeScheduleLists(long scheduleId, List<ScheduleList
}
public void deleteScheduleListsByScheduleId(long scheduleId){
repository.deleteAllByScheduleScheduleId(scheduleId);
List<ScheduleList> findScheduleLists = repository.findAllByScheduleScheduleId(scheduleId);
/*List<ScheduleList> findScheduleLists = repository.findAllByScheduleScheduleId(scheduleId);
findScheduleLists.stream()
.forEach(scheduleList -> repository.delete(scheduleList));
.forEach(scheduleList -> repository.delete(scheduleList));*/
}
public List<ScheduleList> findSchduleListByScheduleId(long scheduleId){
return repository.findAllByScheduleScheduleId(scheduleId);
Expand Down

0 comments on commit 99c39d2

Please sign in to comment.