Skip to content

Commit

Permalink
Merge pull request #129 from MADA-UMC/server-lipton
Browse files Browse the repository at this point in the history
Fix : Calendar Bug
  • Loading branch information
honeybears authored Mar 5, 2024
2 parents 09ef273 + f0ef56b commit f59a320
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ public CalendarController(CalendarService calendarService) {

@GetMapping("/")
ResponseEntity<Map<String,Object>> calendarRead(Authentication authentication){

return ResponseEntity.ok(calendarService.readCalendars(authentication));
}

@PostMapping("/add") //로그인 구현 이후 토큰으로 사용
ResponseEntity<Map<String,Object>> calendarAdd(Authentication authentication, @RequestBody CalendarRequestDto calendarDto){
Map<String,Object> map ;
Map<String,Object> data = new LinkedHashMap<>();
Map<String,Object> map;
map = calendarService.createCalendar(authentication,calendarDto);

return ResponseEntity.ok(map);
}
@PatchMapping("/edit/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.umc.mada.calendar.domain.Calendar;
import com.umc.mada.user.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
Expand All @@ -17,4 +18,13 @@ public interface CalendarRepository extends JpaRepository<Calendar,Long> {
Optional<Calendar> findCalendarByUserAndId(User user, Long id);
List<Calendar> findCalendarsByUserAndStartDateLessThanEqualAndEndDateGreaterThanEqual(User user, LocalDate start_date, LocalDate end_date);

@Query(
"select c " +
"from Calendar c " +
"where c.user = :user " +
"and c.isExpired = false "+
"and YEAR(c.startDate) <= :y and YEAR(c.endDate) >= :y " +
"and MONTH(c.startDate) <= :m and MONTH(c.endDate) >= :m "
)
List<Calendar> findCalendarMonth(User user , int y, int m);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,8 @@ public Map<String, Object> readDday(Authentication authentication){

public Map<String, Object> readMonthCalendar(Authentication authentication, int year,int month){
User user = this.getUser(authentication);
LocalDate start_date = LocalDate.of(year,month,1);

int days = start_date.lengthOfMonth();
LocalDate end_date = LocalDate.of(year,month,days);
List<Calendar> calendarList = calendarRepository.findCalendarsByUserAndStartDateLessThanEqualAndEndDateGreaterThanEqual(user,start_date,start_date);
calendarList = calendarList.stream().filter(calendar->!calendar.isExpired()).collect(Collectors.toList());


List<Calendar> calendarList = calendarRepository.findCalendarMonth(user,year,month);
List<CalendarResponseDto> calendarResponseDtoList = new ArrayList<>();

for (Calendar calendar: calendarList) {
Expand Down
104 changes: 104 additions & 0 deletions src/test/java/com/umc/mada/CalendarJpaTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.umc.mada;

import com.umc.mada.calendar.domain.Calendar;
import com.umc.mada.calendar.repository.CalendarRepository;
import com.umc.mada.user.domain.User;
import com.umc.mada.user.repository.UserRepository;
import net.bytebuddy.utility.RandomString;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@TestMethodOrder(value = MethodOrderer.OrderAnnotation.class)
public class CalendarJpaTest {
@Autowired
private CalendarRepository calendarRepository;

@Autowired
private UserRepository userRepository;

private User user;
private Calendar calendar1;
private Calendar calendar2;

@BeforeEach
public void setUp(){
user = userRepository.findUserById(5L).get();
String randomCode = RandomString.make(15);
String randomName1 = RandomString.make(6);
String randomName2 = RandomString.make(7);
calendar1 = Calendar.builder()
.user(user)
.calendarName(randomName1)
.color("#FDA4B4")
.startDate(LocalDate.of(2024,2,13))
.endDate(LocalDate.of(2024,3,13))
.startTime(LocalTime.of(10,0,0))
.endTime(LocalTime.of(10,0,0))
.memo(randomCode)
.dday('N')
.build();
calendar2 = Calendar.builder()
.user(user)
.calendarName(randomName2)
.color("#FDA4B4")
.startDate(LocalDate.of(2024,5,13))
.endDate(LocalDate.of(2024,6,13))
.startTime(LocalTime.of(10,0,0))
.endTime(LocalTime.of(10,0,0))
.memo(randomCode)
.dday('N')
.build();
calendarRepository.save(calendar1);
calendarRepository.save(calendar2);
System.out.println("Calendar1 name : " + calendar1.getCalendarName());
System.out.println("Calendar2 name : " + calendar1.getCalendarName());
}

@DisplayName("달별 조회 테스트 1")
@Test
@Order(1)
public void LoadByMonthTest1(){
List<Calendar> calendars = calendarRepository.findCalendarMonth(user,2024,2);
for (Calendar c: calendars) {
System.out.println("List Calendar Objects name : "+ c.getCalendarName());
}

assert(calendars.contains(calendar1));
assert(!calendars.contains(calendar2));
}
@DisplayName("달별 조회 테스트 2")
@Test
@Order(2)
public void LoadByMonthTest2(){
List<Calendar> calendars = calendarRepository.findCalendarMonth(user,2024,5);

for (Calendar c: calendars) {
System.out.println("List Calendar Objects name : "+ c.getCalendarName());
}

assert(!calendars.contains(calendar1));
assert(calendars.contains(calendar2));
}
@DisplayName("달별 조회 테스트 3")
@Test
@Order(3)
public void LoadByMonthTest3(){
List<Calendar> calendars = calendarRepository.findCalendarMonth(user,2024,10);

for (Calendar c: calendars) {
System.out.println("List Calendar Objects name : "+ c.getCalendarName());
}

assert(calendars.isEmpty());
}


}

0 comments on commit f59a320

Please sign in to comment.