1. Delete
- detail.mustache
<a href="/news/{{newsId}}/delete" class="btn btn-danger">Delete</a>
- NewsController.class
@GetMapping("/{newsId}/delete")
public String deleteNews(@PathVariable("newsId") Long newsId){
newsRepository.deleteById(newsId);
return "redirect:/news/list";
}
2. Edit
2-1. id로 데이터를 form에 가져오기
- detail.mustache
<a href="/news/{{newsId}}/edit" class="btn btn-secondary">Edit</a>
- NewsController.class
@GetMapping("/{newsId}/edit")
public String editNewsForm(@PathVariable("newsId") Long newsId, Model model) {
News news = newsRepository.findById(newsId)
.orElseThrow(() -> new IllegalArgumentException("해당 뉴스가 존재하지 않습니다."));
model.addAttribute("news", news);
return "news/edit";
}
- edit.mustache 생성
- <input type="hidden" name="newsId" value="{{newsId}}">로 Id를 다시 가져오기
{{>layouts/header}}
{{#news}}
<form class="container" action="/news/{{newsId}}/update" method="post">
<input type="hidden" name="newsId" value="{{newsId}}">
<div class ="mb-3">
<label class="form-label">Title</label>
<input type="text" class="form-control" name="title" value="{{title}}">
</div>
<div class ="mb-3">
<label class="form-label">Content</label>
<textarea class="form-control" name="content" rows="5">{{content}}</textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<a href="/news/{{newsId}}" class="btn btn-danger">Cancel</a>
</form>
{{/news}}
{{>layouts/footer}}
2-2. dto 생성
- NewsDto.class에 Patch 추가
package com.example.news.dto;
import lombok.*;
public class NewsDto {
@Getter
@Setter
@ToString // 주소가 아닌 NewsDto.Post(title=Title, content=속보입니다.)로 보이게 하는 것
@NoArgsConstructor
@AllArgsConstructor
public static class Post{
private String title;
private String content;
}
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public static class Patch {
private String title;
private String content;
}
}
- NewsController.class
@PostMapping("/{newsId}/update")
public String editNews(@PathVariable("newsId") Long newsId, NewsDto.Patch patch) {
News news = newsRepository.findById(newsId)
.orElseThrow(() -> new IllegalArgumentException("해당 뉴스가 존재하지 않습니다."));
news.setTitle(patch.getTitle());
news.setContent(patch.getContent());
newsRepository.save(news);
return "redirect:/news/" + news.getNewsId();
}
- mapper가 있다면?
더보기
- NewsMapper.interface 수정
PatchDtoToNews 추가 > Dto를 News객체로 변환
package com.example.news.mapper;
import com.example.news.domain.News;
import com.example.news.dto.NewsDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.NullValuePropertyMappingStrategy;
import javax.sound.midi.Patch;
@Mapper(componentModel = "spring", nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
public interface NewsMapper{
@Mapping(target = "newsId",ignore = true)
News newsPostDtoToNews(NewsDto.Post post);
@Mapping(target = "newsId", ignore = true)
void PatchDtoToNews(NewsDto.Patch patch, @MappingTarget News news);
//@Override
//public void PatchDtoToNews(Patch patch, News news) {
// if ( patch == null ) {
// return;
// }
// if ( patch.getTitle() != null ) {
// news.setTitle( patch.getTitle() );
// }
// if ( patch.getContent() != null ) {
// news.setContent( patch.getContent() );
// }
//}
}
- NewsController.class
@PostMapping("/{newsId}/update")
public String editNews(@PathVariable("newsId") Long newsId, NewsDto.Patch patch) {
News news = newsRepository.findById(newsId)
.orElseThrow(() -> new IllegalArgumentException("해당 뉴스가 존재하지 않습니다."));
mapper.PatchDtoToNews(patch, news);
newsRepository.save(news);
return "redirect:/news/" + news.getNewsId();
}