Skip to content

Commit

Permalink
✨ [Feature] Admin 공유일정 조회 API #1063 (#1084)
Browse files Browse the repository at this point in the history
  • Loading branch information
seyeon22222 authored Dec 30, 2024
1 parent c84ca7f commit 608f83a
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import gg.data.calendar.PublicSchedule;
import gg.data.calendar.type.DetailClassification;

@Repository
public interface PublicScheduleAdminRepository extends JpaRepository<PublicSchedule, Long> {

List<PublicSchedule> findByAuthor(String author);

Page<PublicSchedule> findAllByClassification(DetailClassification detailClassification, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import gg.calendar.api.admin.schedule.publicschedule.controller.request.PublicScheduleAdminCreateReqDto;
import gg.calendar.api.admin.schedule.publicschedule.controller.response.PublicScheduleAdminResDto;
import gg.calendar.api.admin.schedule.publicschedule.service.PublicScheduleAdminService;
import gg.data.calendar.type.DetailClassification;
import gg.utils.dto.PageRequestDto;
import gg.utils.dto.PageResponseDto;
import lombok.RequiredArgsConstructor;

@RestController
Expand All @@ -27,4 +34,15 @@ public ResponseEntity<Void> publicScheduleCreate(
return ResponseEntity.status(HttpStatus.CREATED).build();
}

@GetMapping("/list/{detailClassification}")
public ResponseEntity<PageResponseDto<PublicScheduleAdminResDto>> publicScheduleAdminClassificationList(
@PathVariable DetailClassification detailClassification, @ModelAttribute PageRequestDto pageRequestDto) {
int page = pageRequestDto.getPage();
int size = pageRequestDto.getSize();

PageResponseDto<PublicScheduleAdminResDto> pageResponseDto = publicScheduleAdminService.findAllByClassification(
detailClassification, page, size);

return ResponseEntity.ok(pageResponseDto);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,66 @@
package gg.calendar.api.admin.schedule.publicschedule.controller.response;

import java.time.LocalDateTime;

import gg.data.calendar.PublicSchedule;
import gg.data.calendar.type.DetailClassification;
import gg.data.calendar.type.EventTag;
import gg.data.calendar.type.JobTag;
import gg.data.calendar.type.ScheduleStatus;
import gg.data.calendar.type.TechTag;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PublicScheduleAdminResDto {
private Long id;

private DetailClassification classification;

private EventTag eventTag;

private JobTag jobTag;

private TechTag techTag;

private String author;

private String title;

private LocalDateTime startTime;

private LocalDateTime endTime;

private String link;

private Integer sharedCount;

private ScheduleStatus status;

@Builder
public PublicScheduleAdminResDto(PublicSchedule publicSchedule) {
this.id = publicSchedule.getId();
this.classification = publicSchedule.getClassification();
this.eventTag = publicSchedule.getEventTag();
this.jobTag = publicSchedule.getJobTag();
this.techTag = publicSchedule.getTechTag();
this.author = publicSchedule.getAuthor();
this.title = publicSchedule.getTitle();
this.startTime = publicSchedule.getStartTime();
this.endTime = publicSchedule.getEndTime();
this.link = publicSchedule.getLink();
this.sharedCount = publicSchedule.getSharedCount();
this.status = publicSchedule.getStatus();
}

@Override
public String toString() {
return "PublicScheduleAdminResDto{" + "id=" + id + ", classification=" + classification + ", eventTag="
+ eventTag + ", jobTag=" + jobTag + ", techTag=" + techTag + ", author='" + author + '\'' + ", title='"
+ title + '\'' + ", startTime=" + startTime + ", endTime=" + endTime + ", link='" + link + '\''
+ ", sharedCount=" + sharedCount + ", status=" + status + '}';
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package gg.calendar.api.admin.schedule.publicschedule.service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import gg.admin.repo.calendar.PublicScheduleAdminRepository;
import gg.calendar.api.admin.schedule.publicschedule.controller.request.PublicScheduleAdminCreateReqDto;
import gg.calendar.api.admin.schedule.publicschedule.controller.response.PublicScheduleAdminResDto;
import gg.data.calendar.PublicSchedule;
import gg.data.calendar.type.DetailClassification;
import gg.utils.dto.PageResponseDto;
import gg.utils.exception.ErrorCode;
import gg.utils.exception.custom.CustomRuntimeException;
import lombok.RequiredArgsConstructor;
Expand All @@ -29,6 +38,21 @@ public void createPublicSchedule(PublicScheduleAdminCreateReqDto publicScheduleA
publicScheduleAdminRepository.save(publicSchedule);
}

public PageResponseDto<PublicScheduleAdminResDto> findAllByClassification(
DetailClassification detailClassification, int page, int size) {

Pageable pageable = PageRequest.of(page - 1, size,
Sort.by(Sort.Order.asc("status"), Sort.Order.asc("startTime")));

Page<PublicSchedule> publicSchedules = publicScheduleAdminRepository.findAllByClassification(
detailClassification, pageable);

List<PublicScheduleAdminResDto> publicScheduleList = publicSchedules.stream()
.map(PublicScheduleAdminResDto::new)
.collect(Collectors.toList());
return PageResponseDto.of(publicSchedules.getTotalElements(), publicScheduleList);
}

private void dateTimeErrorCheck(LocalDateTime startTime, LocalDateTime endTime) {
if (startTime.isAfter(endTime)) {
throw new CustomRuntimeException(ErrorCode.CALENDAR_BEFORE_DATE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import org.springframework.stereotype.Component;

import gg.admin.repo.calendar.PublicScheduleAdminRepository;
import gg.calendar.api.admin.schedule.publicschedule.controller.request.PublicScheduleAdminCreateReqDto;
import gg.data.calendar.PublicSchedule;
import gg.data.calendar.type.DetailClassification;
import gg.data.calendar.type.EventTag;
import gg.data.calendar.type.JobTag;
import gg.data.calendar.type.ScheduleStatus;
import lombok.RequiredArgsConstructor;

@Component
Expand All @@ -35,4 +36,53 @@ public PublicSchedule createPublicSchedule() {
return publicScheduleAdminRepository.save(publicSchedule);
}

public void createPublicScheduleEvent(int size) {
for (int i = 0; i < size; i++) {
PublicSchedule publicSchedule = PublicSchedule.builder()
.classification(DetailClassification.EVENT)
.eventTag(EventTag.JOB_FORUM)
.author("42GG")
.title("Job " + i)
.content("TEST JOB")
.link("https://gg.42seoul.kr")
.status(ScheduleStatus.ACTIVATE)
.startTime(LocalDateTime.now().plusDays(i))
.endTime(LocalDateTime.now().plusDays(i + 10))
.build();
publicScheduleAdminRepository.save(publicSchedule);
}
}

public void createPublicScheduleJob(int size) {
for (int i = 0; i < size; i++) {
PublicSchedule publicSchedule = PublicSchedule.builder()
.classification(DetailClassification.JOB_NOTICE)
.jobTag(JobTag.EXPERIENCED)
.author("42GG")
.title("Job " + i)
.content("TEST JOB")
.link("https://gg.42seoul.kr")
.status(ScheduleStatus.ACTIVATE)
.startTime(LocalDateTime.now().plusDays(i))
.endTime(LocalDateTime.now().plusDays(i + 10))
.build();
publicScheduleAdminRepository.save(publicSchedule);
}
}

public void createPublicSchedulePrivate(int size) {
for (int i = 0; i < size; i++) {
PublicSchedule publicSchedule = PublicSchedule.builder()
.classification(DetailClassification.PRIVATE_SCHEDULE)
.author("42GG")
.title("Private " + i)
.content("TEST Private")
.link("https://gg.42seoul.kr")
.status(ScheduleStatus.ACTIVATE)
.startTime(LocalDateTime.now().plusDays(i))
.endTime(LocalDateTime.now().plusDays(i + 10))
.build();
publicScheduleAdminRepository.save(publicSchedule);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Stream;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;
Expand All @@ -15,16 +16,24 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import gg.admin.repo.calendar.PublicScheduleAdminRepository;
import gg.calendar.api.admin.PublicScheduleAdminMockData;
import gg.calendar.api.admin.schedule.publicschedule.controller.request.PublicScheduleAdminCreateReqDto;
import gg.calendar.api.admin.schedule.publicschedule.controller.response.PublicScheduleAdminResDto;
import gg.calendar.api.admin.schedule.publicschedule.service.PublicScheduleAdminService;
import gg.data.calendar.PublicSchedule;
import gg.data.calendar.type.DetailClassification;
Expand All @@ -34,6 +43,7 @@
import gg.data.user.User;
import gg.utils.TestDataUtils;
import gg.utils.annotation.IntegrationTest;
import gg.utils.dto.PageResponseDto;
import lombok.extern.slf4j.Slf4j;

@Slf4j
Expand Down Expand Up @@ -170,4 +180,55 @@ public void createPublicScheduleContentMax() throws Exception {
.andExpect(status().isBadRequest());
}
}

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Nested
@DisplayName("Admin PublicSchedule 태그 조회 테스트")
class GetPublicScheduleAdminClassificationListTest {

private Stream<Arguments> inputParams() {
return Stream.of(Arguments.of("EVENT", 2, 10), Arguments.of("JOB_NOTICE", 1, 10),
Arguments.of("PRIVATE_SCHEDULE", 1, 2));
}

@ParameterizedTest
@MethodSource("inputParams")
@DisplayName("Admin PublicSchedule 태그 조회 테스트 - 성공")
void getPublicScheduleAdminClassificationListTestSuccess(String tags, int page, int size) throws
Exception {
// given
publicScheduleAdminMockData.createPublicScheduleEvent(20);
publicScheduleAdminMockData.createPublicScheduleJob(10);
publicScheduleAdminMockData.createPublicSchedulePrivate(5);

MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("page", String.valueOf(page));
params.add("size", String.valueOf(size));

// when
// multivalue map 을 통해서 값이 넘어옴
String response = mockMvc.perform(
get("/admin/calendar/public/list/{detailClassification}", tags).header("Authorization",
"Bearer " + accessToken)
.params(params))
.andDo(print())
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();

// then
PageResponseDto<PublicScheduleAdminResDto> pageResponseDto = objectMapper.readValue(
response, new TypeReference<>() {
});
List<PublicScheduleAdminResDto> result = pageResponseDto.getContent();

if (DetailClassification.valueOf(tags) == DetailClassification.PRIVATE_SCHEDULE) {
assertThat(result.size()).isEqualTo(2);
} else {
assertThat(result.size()).isEqualTo(10);
}

for (PublicScheduleAdminResDto dto : result) {
System.out.println(dto.toString());
}
}
}
}

0 comments on commit 608f83a

Please sign in to comment.