mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-12 10:00:58 +08:00
feat: add dedicated mappers
This commit is contained in:
@@ -2,6 +2,7 @@ package com.openisle.controller;
|
||||
|
||||
import com.openisle.dto.DraftDto;
|
||||
import com.openisle.dto.DraftRequest;
|
||||
import com.openisle.mapper.DraftMapper;
|
||||
import com.openisle.model.Draft;
|
||||
import com.openisle.service.DraftService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -9,24 +10,23 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/drafts")
|
||||
@RequiredArgsConstructor
|
||||
public class DraftController {
|
||||
private final DraftService draftService;
|
||||
private final DraftMapper draftMapper;
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<DraftDto> saveDraft(@RequestBody DraftRequest req, Authentication auth) {
|
||||
Draft draft = draftService.saveDraft(auth.getName(), req.getCategoryId(), req.getTitle(), req.getContent(), req.getTagIds());
|
||||
return ResponseEntity.ok(toDto(draft));
|
||||
return ResponseEntity.ok(draftMapper.toDto(draft));
|
||||
}
|
||||
|
||||
@GetMapping("/me")
|
||||
public ResponseEntity<DraftDto> getMyDraft(Authentication auth) {
|
||||
return draftService.getDraft(auth.getName())
|
||||
.map(d -> ResponseEntity.ok(toDto(d)))
|
||||
.map(d -> ResponseEntity.ok(draftMapper.toDto(d)))
|
||||
.orElseGet(() -> ResponseEntity.noContent().build());
|
||||
}
|
||||
|
||||
@@ -35,17 +35,4 @@ public class DraftController {
|
||||
draftService.deleteDraft(auth.getName());
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
private DraftDto toDto(Draft draft) {
|
||||
DraftDto dto = new DraftDto();
|
||||
dto.setId(draft.getId());
|
||||
dto.setTitle(draft.getTitle());
|
||||
dto.setContent(draft.getContent());
|
||||
if (draft.getCategory() != null) {
|
||||
dto.setCategoryId(draft.getCategory().getId());
|
||||
}
|
||||
dto.setTagIds(draft.getTags().stream().map(com.openisle.model.Tag::getId).collect(Collectors.toList()));
|
||||
return dto;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user