Skip to content

Commit

Permalink
익명의 메모 만들기 24시간 이내 조회 포함 완료_13일차
Browse files Browse the repository at this point in the history
  • Loading branch information
Kim-HJ1986 committed May 21, 2022
1 parent 3ff0556 commit b3f373b
Show file tree
Hide file tree
Showing 11 changed files with 522 additions and 1 deletion.
3 changes: 3 additions & 0 deletions week03/src/main/java/com/spring/week03/Week03Application.java
@@ -1,8 +1,11 @@
package com.spring.week03;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@EnableJpaAuditing // JPA auditing 넣어줘야함!
@SpringBootApplication
public class Week03Application {

Expand Down
@@ -0,0 +1,45 @@
package com.spring.week03.controller;

import com.spring.week03.domain.Memo;
import com.spring.week03.domain.MemoRepository;
import com.spring.week03.domain.MemoRequestDto;
import com.spring.week03.service.MemoService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

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

@RequiredArgsConstructor
@RestController
public class MemoController {

private final MemoRepository memoRepository;
private final MemoService memoService;



@PostMapping("/api/memos")
public Memo createMemo(@RequestBody MemoRequestDto requestDto){
Memo memo = new Memo(requestDto);
return memoRepository.save(memo);
}

@GetMapping("/api/memos")
public List<Memo> readMemo(){
LocalDateTime start = LocalDateTime.now().minusDays(1);
LocalDateTime end = LocalDateTime.now();
return memoRepository.findAllByModifiedAtBetweenOrderByModifiedAtDesc(start, end);
}

@PutMapping("/api/memos/{id}")
public Long updateMemo(@PathVariable Long id, @RequestBody MemoRequestDto requestDto){
return memoService.update(id, requestDto);
}

@DeleteMapping("/api/memos/{id}")
public Long deleteMemo(@PathVariable Long id){
memoRepository.deleteById(id);
return id;
}
}
5 changes: 5 additions & 0 deletions week03/src/main/java/com/spring/week03/domain/Memo.java
Expand Up @@ -28,4 +28,9 @@ public Memo(MemoRequestDto requestDto) {
this.username = requestDto.getUsername();
this.contents = requestDto.getContents();
}

public void update(MemoRequestDto requestDto) {
this.username = requestDto.getUsername();
this.contents = requestDto.getContents();
}
}
Expand Up @@ -2,11 +2,12 @@

import org.springframework.data.jpa.repository.JpaRepository;

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

public interface MemoRepository extends JpaRepository<Memo, Long> {
// JPA의 메서드는 이름에 따라 기능을 바꿀 수 있다. 북마크에 저장된 문서 참조!
// Memo class가 Timestamped를 상속받고 있기 때문에 ModifiedAt을 사용할 수 있는 것이다.
// 아래 메서드는 모든 것을 찾되, 수정된 날짜를 역순으로 정렬해서 찾아라는 뜻!
List<Memo> findAllByOrderByModifiedAtDesc();
List<Memo> findAllByModifiedAtBetweenOrderByModifiedAtDesc(LocalDateTime startDate, LocalDateTime endDate);
}
@@ -1,5 +1,6 @@
package com.spring.week03.domain;

import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
Expand All @@ -8,6 +9,7 @@
import javax.persistence.MappedSuperclass;
import java.time.LocalDateTime;

@Getter // private일 때는 Getter 꼭 써줘야한다!
@MappedSuperclass // Entity가 자동으로 컬럼으로 인식합니다.
@EntityListeners(AuditingEntityListener.class) // 생성/변경 시간을 자동으로 업데이트합니다.
public class Timestamped {
Expand Down
25 changes: 25 additions & 0 deletions week03/src/main/java/com/spring/week03/service/MemoService.java
@@ -0,0 +1,25 @@
package com.spring.week03.service;

import com.spring.week03.domain.Memo;
import com.spring.week03.domain.MemoRepository;
import com.spring.week03.domain.MemoRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@RequiredArgsConstructor
@Service
public class MemoService {

private final MemoRepository memoRepository;

@Transactional // DB에 반영시켜줘!
public Long update(Long id, MemoRequestDto requestDto) {
Memo memo = memoRepository.findById(id).orElseThrow(
() -> new IllegalArgumentException("아이디가 존재하지 않습니다.")
);
memo.update(requestDto);
return memo.getId();
}
}
Binary file added week03/src/main/resources/static/images/delete.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added week03/src/main/resources/static/images/done.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added week03/src/main/resources/static/images/edit.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added week03/src/main/resources/static/images/send.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b3f373b

Please sign in to comment.