📅 2024년 01월 22일
(안보고 해석하기)
Domain
- Author
@Entity @Getter @AllArgsConstructor @NoArgsConstructor public class Author { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false, length = 20) private String name; @Column(nullable = false, length = 20, unique = true) private String email; @Column(nullable = false) private String password; @Enumerated(value = EnumType.STRING) private Role role; @CreationTimestamp @Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP") LocalDateTime createdTime; @UpdateTimestamp @Column(columnDefinition = "TIMESTAMP ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP") LocalDateTime updatedTime; public Author(String name, String email, String password){ this.name = name; this.email = email; this.password = password; } }
- Role
public enum Role { ADMIN, USER }
Controller
- AuthorController
@Controller public class AuthorController { private final AuthorService authorService; @Autowired public AuthorController(AuthorService authorService){ this.authorService = authorService; } @GetMapping("/") public String home(){ return "/home"; } @PostMapping("/author/save") @ResponseBody public String authorSave(AuthorSaveDto authorSaveDto){ authorService.save(authorSaveDto); return "ok"; } @GetMapping("/author/list") public String authorList(Model model){ model.addAttribute("authorList", authorService.findAll()); return "/author/author-list"; } @GetMapping("/author/detail/{id}") public String authorDetail(@PathVariable long id, Model model) { try { model.addAttribute("author",authorService.findById(id)); return "/author/author-detail"; }catch (Exception e){ return "redirect:/author/list"; } } }
Dto
- AuthorDetailResDto
@Data public class AuthorDetailResDto { private Long id; private String name; private String email; private String password; private LocalDateTime createdTime; }
- AuthorListResDto
@Data public class AuthorListResDto { private Long id; private String name; private String email; }
- AuthorSaveDto
@Data public class AuthorSaveDto { private String name; private String email; private String password; }
Repository
- AuthorRepository
@Repository public interface AuthorRepository extends JpaRepository<Author,Long> { }
Service
- AuthorService
@Service public class AuthorService { private final AuthorRepository authorRepository; @Autowired public AuthorService(AuthorRepository authorRepository){ this.authorRepository = authorRepository; } public void save(AuthorSaveDto authorSaveDto){ Author author = new Author( authorSaveDto.getName(), authorSaveDto.getEmail(), authorSaveDto.getPassword() ); authorRepository.save(author); } public List<AuthorListResDto> findAll(){ List<Author> authorList = authorRepository.findAll(); List<AuthorListResDto> listResDtos = new ArrayList<>(); for(Author author : authorList){ AuthorListResDto authorListResDto = new AuthorListResDto(); authorListResDto.setId(author.getId()); authorListResDto.setName(author.getName()); authorListResDto.setEmail(author.getEmail()); listResDtos.add(authorListResDto); } return listResDtos; } public AuthorDetailResDto findById(long id) throws EntityNotFoundException { Author author = authorRepository.findById(id).orElseThrow(EntityNotFoundException::new); AuthorDetailResDto authorDetailResDto = new AuthorDetailResDto(); authorDetailResDto.setId(author.getId()); authorDetailResDto.setName(author.getName()); authorDetailResDto.setEmail(author.getEmail()); authorDetailResDto.setPassword(author.getPassword()); authorDetailResDto.setCreatedTime(author.getCreatedTime()); return authorDetailResDto; } }