-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support announcement create, get
- Loading branch information
Showing
11 changed files
with
389 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/com/akagiyui/drive/controller/AnnouncementController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.akagiyui.drive.controller; | ||
|
||
import com.akagiyui.drive.entity.Announcement; | ||
import com.akagiyui.drive.model.request.AddAnnouncementRequest; | ||
import com.akagiyui.drive.model.response.AnnouncementDisplayResponse; | ||
import com.akagiyui.drive.model.response.AnnouncementResponse; | ||
import com.akagiyui.drive.service.AnnouncementService; | ||
import com.akagiyui.drive.service.UserService; | ||
import jakarta.annotation.Resource; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 公告控制器 | ||
* | ||
* @author AkagiYui | ||
*/ | ||
@RestController | ||
@RequestMapping("/announcement") | ||
public class AnnouncementController { | ||
|
||
@Resource | ||
private AnnouncementService announcementService; | ||
|
||
@Resource | ||
private UserService userService; | ||
|
||
/** | ||
* 新增公告 | ||
*/ | ||
@PostMapping({"", "/"}) | ||
public Boolean addAnnouncement(@RequestBody @Validated AddAnnouncementRequest request) { | ||
// todo 权限校验 | ||
Announcement announcement = request.toAnnouncement(); | ||
announcement.setAuthor(userService.getUser()); | ||
return announcementService.addAnnouncement(announcement); | ||
} | ||
|
||
/** | ||
* 获取公告列表 | ||
*/ | ||
@GetMapping({"", "/"}) | ||
public List<AnnouncementResponse> getAnnouncementList(@RequestParam(defaultValue = "false") Boolean all) { | ||
// todo 权限校验 | ||
return AnnouncementResponse.fromAnnouncementList(announcementService.getAnnouncementList(all)); | ||
} | ||
|
||
/** | ||
* 获取用于首页展示的公告列表 | ||
*/ | ||
@GetMapping("/index") | ||
public List<AnnouncementDisplayResponse> getIndexAnnouncementList() { | ||
return AnnouncementDisplayResponse.fromAnnouncementList(announcementService.getAnnouncementDisplayList()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.akagiyui.drive.entity; | ||
|
||
import com.akagiyui.common.entity.BaseEntity; | ||
import jakarta.persistence.*; | ||
import lombok.Data; | ||
import lombok.ToString; | ||
import lombok.experimental.Accessors; | ||
import org.hibernate.annotations.ColumnDefault; | ||
import org.hibernate.annotations.DynamicInsert; | ||
|
||
|
||
/** | ||
* 公告实体 | ||
* | ||
* @author AkagiYui | ||
*/ | ||
@Data | ||
@ToString(exclude = "author") | ||
@Accessors(chain = true) | ||
@Entity | ||
@Table | ||
@DynamicInsert | ||
public class Announcement extends BaseEntity { | ||
/** | ||
* 标题 | ||
*/ | ||
@Column(nullable = false) | ||
private String title; | ||
|
||
/** | ||
* 内容 | ||
*/ | ||
private String content; | ||
|
||
/** | ||
* 已启用 | ||
*/ | ||
@Column(nullable = false) | ||
@ColumnDefault("true") | ||
private Boolean enabled; | ||
|
||
/** | ||
* 发布者 | ||
*/ | ||
@ManyToOne(fetch = FetchType.EAGER) | ||
@JoinColumn(name = "user_id") | ||
private User author; | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/akagiyui/drive/model/request/AddAnnouncementRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.akagiyui.drive.model.request; | ||
|
||
import com.akagiyui.drive.entity.Announcement; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Data; | ||
|
||
/** | ||
* 新增公告 请求 | ||
* | ||
* @author AkagiYui | ||
*/ | ||
@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); | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/com/akagiyui/drive/model/response/AnnouncementDisplayResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.akagiyui.drive.model.response; | ||
|
||
import com.akagiyui.drive.entity.Announcement; | ||
import lombok.Data; | ||
import lombok.experimental.Accessors; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* 公告信息 响应 | ||
* | ||
* @author AkagiYui | ||
*/ | ||
@Data | ||
@Accessors(chain = true) | ||
public class AnnouncementDisplayResponse { | ||
|
||
/** | ||
* 标题 | ||
*/ | ||
private String title; | ||
|
||
/** | ||
* 内容 | ||
*/ | ||
private String content; | ||
|
||
/** | ||
* 发布者昵称 | ||
*/ | ||
private String userNickname; | ||
|
||
/** | ||
* 发布时间 | ||
*/ | ||
private Date createTime; | ||
|
||
/** | ||
* 修改时间 | ||
*/ | ||
private Date updateTime; | ||
|
||
/** | ||
* 从公告实体转换 | ||
*/ | ||
public static AnnouncementDisplayResponse fromAnnouncement(Announcement announcement) { | ||
return new AnnouncementDisplayResponse() | ||
.setTitle(announcement.getTitle()) | ||
.setContent(announcement.getContent()) | ||
.setUserNickname(announcement.getAuthor().getNickname()) | ||
.setCreateTime(announcement.getCreateTime()) | ||
.setUpdateTime(announcement.getUpdateTime()); | ||
} | ||
|
||
/** | ||
* 从公告实体列表转换 | ||
*/ | ||
public static List<AnnouncementDisplayResponse> fromAnnouncementList(List<Announcement> announcementList) { | ||
return announcementList.stream() | ||
.map(AnnouncementDisplayResponse::fromAnnouncement) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/com/akagiyui/drive/model/response/AnnouncementResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.akagiyui.drive.model.response; | ||
|
||
import com.akagiyui.drive.entity.Announcement; | ||
import lombok.Data; | ||
import lombok.experimental.Accessors; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* 公告信息 响应 | ||
* | ||
* @author AkagiYui | ||
*/ | ||
@Data | ||
@Accessors(chain = true) | ||
public class AnnouncementResponse { | ||
|
||
/** | ||
* 公告id | ||
*/ | ||
private String id; | ||
|
||
/** | ||
* 标题 | ||
*/ | ||
private String title; | ||
|
||
/** | ||
* 内容 | ||
*/ | ||
private String content; | ||
|
||
/** | ||
* 用户ID | ||
*/ | ||
private String userId; | ||
|
||
/** | ||
* 已启用 | ||
*/ | ||
private Boolean enabled; | ||
|
||
/** | ||
* 发布时间 | ||
*/ | ||
private Date createTime; | ||
|
||
/** | ||
* 修改时间 | ||
*/ | ||
private Date updateTime; | ||
|
||
/** | ||
* 从公告实体转换 | ||
*/ | ||
public static AnnouncementResponse fromAnnouncement(Announcement announcement) { | ||
return new AnnouncementResponse() | ||
.setId(announcement.getId()) | ||
.setTitle(announcement.getTitle()) | ||
.setContent(announcement.getContent()) | ||
.setUserId(announcement.getAuthor().getId()) | ||
.setEnabled(announcement.getEnabled()) | ||
.setCreateTime(announcement.getCreateTime()) | ||
.setUpdateTime(announcement.getUpdateTime()); | ||
} | ||
|
||
/** | ||
* 从公告实体列表转换 | ||
*/ | ||
public static List<AnnouncementResponse> fromAnnouncementList(List<Announcement> announcementList) { | ||
return announcementList.stream() | ||
.map(AnnouncementResponse::fromAnnouncement) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/akagiyui/drive/repository/AnnouncementRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.akagiyui.drive.repository; | ||
|
||
import com.akagiyui.drive.entity.Announcement; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 公告 操作接口 | ||
* | ||
* @author AkagiYui | ||
*/ | ||
public interface AnnouncementRepository extends JpaRepository<Announcement, String> { | ||
|
||
/** | ||
* 查询所有启用的公告 | ||
*/ | ||
List<Announcement> findAnnouncementsByEnabledIsTrue(); | ||
|
||
/** | ||
* 查询所有启用的公告并按更新时间倒序排序 | ||
*/ | ||
List<Announcement> findAnnouncementsByEnabledIsTrueOrderByUpdateTimeDesc(); | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/akagiyui/drive/service/AnnouncementService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.akagiyui.drive.service; | ||
|
||
import com.akagiyui.drive.entity.Announcement; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 公告服务接口 | ||
* | ||
* @author AkagiYui | ||
*/ | ||
public interface AnnouncementService { | ||
|
||
/** | ||
* 新增公告 | ||
*/ | ||
boolean addAnnouncement(Announcement announcement); | ||
|
||
/** | ||
* 获取公告列表 | ||
* | ||
* @param all 是否获取所有公告 | ||
*/ | ||
List<Announcement> getAnnouncementList(Boolean all); | ||
|
||
/** | ||
* 获取用于首页展示的公告列表 | ||
*/ | ||
List<Announcement> getAnnouncementDisplayList(); | ||
|
||
} |
Oops, something went wrong.