Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在controller层处理model的转换。 #1

Open
AkagiYui opened this issue Aug 26, 2023 · 0 comments
Open

在controller层处理model的转换。 #1

AkagiYui opened this issue Aug 26, 2023 · 0 comments
Labels
good first issue Good for newcomers

Comments

@AkagiYui
Copy link
Owner

AkagiYui commented Aug 26, 2023

模仿AnnouncementController修改其他controller与service,在controller层进行request model和entity,entity和response model的转换。

AnnouncementController.java

    @PostMapping({"", "/"})
    public Boolean addAnnouncement(@RequestBody @Validated AddAnnouncementRequest request) {
        Announcement announcement = request.toAnnouncement();
        announcement.setAuthor(userService.getUser());
        return announcementService.addAnnouncement(announcement);
    }

    @GetMapping({"", "/"})
    public List<AnnouncementResponse> getAnnouncementList(@RequestParam(defaultValue = "false") Boolean all) {
        return AnnouncementResponse.fromAnnouncementList(announcementService.getAnnouncementList(all));
    }

    @GetMapping("/index")
    public List<AnnouncementDisplayResponse> getIndexAnnouncementList() {
        return AnnouncementDisplayResponse.fromAnnouncementList(announcementService.getAnnouncementDisplayList());
    }

AddAnnouncementRequest.java

@Data
public class AddAnnouncementRequest {

    @NotBlank(message = "{title cannot be empty}")
    @NotNull(message = "{title is missing}")
    private String title;

    private String content;

    public Announcement toAnnouncement() {
        return new Announcement()
                .setTitle(title)
                .setContent(content)
                .setEnabled(true);
    }

}

AnnouncementResponse.java

@Data
@Accessors(chain = true)
public class AnnouncementResponse {

    private String id;

    private String title;

    private String content;

    private String userId;

    private Boolean enabled;

    private Date createTime;

    private Date updateTime;

   public AnnouncementResponse(Announcement announcement) {
        this.id = announcement.getId();
        this.title = announcement.getTitle();
        this.content = announcement.getContent();
        this.userId = announcement.getAuthor().getId();
        this.enabled = announcement.getEnabled();
        this.createTime = announcement.getCreateTime();
        this.updateTime = announcement.getUpdateTime();
    }

    public static List<AnnouncementResponse> fromAnnouncementList(List<Announcement> announcementList) {
        return announcementList.stream()
                .map(AnnouncementResponse::new)
                .collect(Collectors.toList());
    }
}

目前涉及到的controller有:RoleControllerUserController

@AkagiYui AkagiYui added the good first issue Good for newcomers label Aug 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant