TIL Java
내일배움캠프 31일차 TIL _ 7주 2일차
mad038
2024. 5. 28. 10:46
- 오늘 있었던 일
- 개인과제
- 알고리즘
SPRIONG
- 스프링 정리
더보기
숙련 스프링 공부
- 댓글 컨트롤러
package com.sparta.calendar.controller; import com.sparta.calendar.dto.ReplyRequestDto; import com.sparta.calendar.dto.ReplyResponseDto; import com.sparta.calendar.service.ReplyService; import io.swagger.v3.oas.annotations.Operation; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/api/") @RequiredArgsConstructor public class ReplyController { private final ReplyService replyService; @PostMapping("days/{todo}/reply") @Operation(summary = "댓글 쓰기") public ReplyResponseDto reply(@RequestBody ReplyRequestDto replydto, @PathVariable String todo) { return replyService.createreply(todo,replydto); } @GetMapping("days/{todo}/reply") @Operation(summary = "댓글 조회") public List<ReplyResponseDto> getReply(@PathVariable String todo) { return replyService.getReply(todo); } @PutMapping("days/{todo}/{reply_id}") @Operation(summary = "댓글 수정") public String updateReply( @PathVariable String todo, @RequestBody ReplyRequestDto replydto, @PathVariable Long reply_id) { return replyService.updateReply(todo,replydto,reply_id); } @DeleteMapping("days/{todo}/{reply_id}") @Operation(summary = "댓글 삭제") public String deleteReply( @PathVariable String todo, @PathVariable Long reply_id) { return replyService.deleteReply(todo,reply_id); } }
- 댓글 서비스
-
package com.sparta.calendar.service; import com.sparta.calendar.dto.CalendarResponseDto; import com.sparta.calendar.dto.ReplyRequestDto; import com.sparta.calendar.dto.ReplyResponseDto; import com.sparta.calendar.entitiy.Calendar; import com.sparta.calendar.entitiy.Reply; import com.sparta.calendar.repository.ReplyRepository; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.util.List; @Service @RequiredArgsConstructor public class ReplyService { private final ReplyRepository replyRepository; private final CalendarService calendarService; @Transactional public ReplyResponseDto createreply(String todo, ReplyRequestDto replydto) { Calendar calendar = calendarService.findCalendarTodo(todo); Reply reply = new Reply(replydto); calendar.getReplylist().add(reply); Reply savereply = replyRepository.save( reply ); return new ReplyResponseDto( savereply ); } @Transactional public String updateReply(String todo, ReplyRequestDto replydto, Long reply_id) { Reply reply = getReply( todo, reply_id ); reply.reply_update(replydto); return "댓글 수정 완료"; } public String deleteReply(String todo, Long reply_id) { Reply reply = getReply( todo, reply_id ); replyRepository.delete( reply ); return "삭제 완료"; } public List<ReplyResponseDto> getReply(String todo) { List<CalendarResponseDto> calendarList = calendarService.getTodo( todo ); if (calendarList.isEmpty()) { throw new IllegalArgumentException( "댓글이 없습니다."); } CalendarResponseDto calendar = calendarList.get( 0 ); return replyRepository.findAll().stream() .filter( R -> R.getCalendar().getId().equals( calendar.getId() ) ) .map( ReplyResponseDto ::new ).toList(); } private Reply getReply(String todo, Long reply_id) { List<ReplyResponseDto> responseDtoList = getReply( todo ).stream() .filter( C -> C.getId().equals( reply_id ) ) .toList(); if (responseDtoList.isEmpty()) { throw new IllegalArgumentException( "해당 댓글이 없습니다."); } return replyRepository.findById( reply_id ) .orElseThrow(() -> new IllegalArgumentException("해당 ID의 댓글이 존재하지 않습니다.")); } }
- N의 관계 수정
-
1의 관계인 Calendar @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.REMOVE}) @JoinColumn(name = "calendar_id") private List<Reply> replylist = new ArrayList<>(); 다수의 관계인 Reply @ManyToOne @JoinColumn(name = "calendar_id" ,insertable = false, updatable = false) private Calendar calendar;
알고리즘 문제 풀기
● 개인정보 수집 유효기간
더보기
● 개인정보 수집 유효기간 //링크
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Solution {
public int[] solution(String today, String[] terms, String[] privacies) {
String[] todo = today.split( "\\." );
int year = Integer.parseInt( todo[0] );
int month = Integer.parseInt( todo[1] );
int day = Integer.parseInt( todo[2] );
int i = 1;
List<Integer> list = new ArrayList<>();
for (String pr : privacies) {
String[] prr = pr.split( "\\.");
String[] prrr = prr[2].split( " " );
int pr_year = Integer.parseInt( prr[0] );
int pr_month = Integer.parseInt( prr[1] );
int pr_day = Integer.parseInt( prrr[0] );
for (String tm : terms) {
String[] z = tm.split( " " );
if (z[0].equals( prrr[1] )) {
int tm_month = Integer.parseInt( z[1] );
pr_day += (tm_month *28) - 1 ;
pr_month += pr_day/28;
while (pr_month > 12) {
pr_month -= 12;
pr_year++;
}
if(pr_day%28 == 0) {
pr_month--;
if(pr_month == 0) {
pr_month = 12;
pr_year--;
}
pr_day = 28;
}else {
pr_day = pr_day%28;
}
if (pr_year < year) {
list.add( i );
break;
} else if (pr_year == year && pr_month < month) {
list.add( i );
break;
}else if (pr_year == year && pr_month == month && pr_day < day) {
list.add( i );
break;
}
}
}
i++;
}
int[] answer = list.stream().mapToInt( e -> e ).toArray();
return answer;
}
}
- 첫 시도때 태스트문도 통과했지만 후반 문제가 전부 틀렸다.
- 문제 지문에
- 유효기간은 개인정보를 보관할 수 있는 달 수를 나타내는 정수이며, 1 이상 100 이하입니다.
- 유효기간이 100 까지 간다?.
- 뭘하든 문제가 틀려서서 지문을 다시 읽어보니 유효기간이 100까지 간다. 기존문에선 if로 12이상일때 한번만 하기에 틀리는 것
-
if (pr_month > 12) { pr_month -= 12; pr_year++; }
- 그렇다면 whlie문으로 만들면 될려나 = 통과했다.
-
while (pr_month > 12) { pr_month -= 12; pr_year++; }
- 문제 지문에
- 밑은 다른 사람의 풀이
-
import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; class Solution { public int[] solution(String today, String[] terms, String[] privacies) { List<Integer> answer = new ArrayList<>(); Map<String, Integer> termMap = new HashMap<>(); int date = getDate(today); for (String s : terms) { String[] term = s.split(" "); termMap.put(term[0], Integer.parseInt(term[1])); } for (int i = 0; i < privacies.length; i++) { String[] privacy = privacies[i].split(" "); if (getDate(privacy[0]) + (termMap.get(privacy[1]) * 28) <= date) { answer.add(i + 1); } } return answer.stream().mapToInt(integer -> integer).toArray(); } private int getDate(String today) { String[] date = today.split("\\."); int year = Integer.parseInt(date[0]); int month = Integer.parseInt(date[1]); int day = Integer.parseInt(date[2]); return (year * 12 * 28) + (month * 28) + day; } }
- 아? 아니 내가 했던 년도나 월별 일별 이렇게 하지않고 그냥 전부 "일"로 만들어서 비교했다. 아니 내가 너무 형식을 지켜야 한다고 생각했던건가. 대단하다
당일 회고
- 할수록 버그가 나온다 으아아