mirror of
https://github.com/HangarMC/Hangar.git
synced 2024-12-27 07:03:26 +08:00
Channel saving
This commit is contained in:
parent
25cf62e383
commit
710030a5e6
@ -50,8 +50,10 @@ public class ChannelsController extends HangarController {
|
||||
|
||||
@Secured("ROLE_USER")
|
||||
@PostMapping("/{author}/{slug}/channels/{channel}")
|
||||
public ModelAndView save(@PathVariable String author, @PathVariable String slug, @PathVariable String channel) {
|
||||
// TODO implement save request controller
|
||||
public ModelAndView save(@PathVariable String author, @PathVariable String slug, @PathVariable String channel,
|
||||
@RequestParam("channel-input") String newChannelName, @RequestParam("channel-color-input") String newChannelHex) {
|
||||
ProjectData projectData = projectService.getProjectData(author, slug);
|
||||
channelService.updateProjectChannel(projectData.getProject().getId(), channel, newChannelName, Color.getByHexStr(newChannelHex));
|
||||
return new ModelAndView("redirect:" + routeHelper.getRouteUrl("channels.showList", author, slug));
|
||||
}
|
||||
|
||||
|
@ -26,8 +26,8 @@ public interface ProjectChannelDao {
|
||||
@GetGeneratedKeys
|
||||
ProjectChannelsTable insert(@BindBean ProjectChannelsTable projectChannel);
|
||||
|
||||
@SqlUpdate("UPDATE (name, color) IN project_channels WHERE project_id = :project_id, id = :id values (:name, :color)")
|
||||
void update(long projectId, long id, String name, @EnumByOrdinal Color color);
|
||||
@SqlUpdate("UPDATE project_channels SET name = :name, color = :color WHERE project_id = :projectId AND name = :oldName")
|
||||
void update(long projectId, String oldName, String name, @EnumByOrdinal Color color);
|
||||
|
||||
@SqlQuery("SELECT * FROM project_channels WHERE project_id = :projectId LIMIT 1")
|
||||
ProjectChannelsTable getFirstChannel(long projectId);
|
||||
@ -44,6 +44,13 @@ public interface ProjectChannelDao {
|
||||
"FROM (SELECT COUNT(*) count FROM project_channels WHERE project_id = :projectId) AS countq")
|
||||
ChannelService.InvalidChannelCreationReason validateChannelCreation(long projectId, @Define String channelName, @Define long colorValue, @Define int maxChannels);
|
||||
|
||||
@UseStringTemplateEngine
|
||||
@SqlQuery("SELECT " +
|
||||
"CASE WHEN '<channelName>' IN (SELECT \"name\" FROM project_channels WHERE project_id = :projectId AND name != :oldChannelName) THEN 'DUPLICATE_NAME' " +
|
||||
" WHEN <colorValue> IN (SELECT color FROM project_channels WHERE project_id = :projectId AND name != :oldChannelName) THEN 'UNIQUE_COLOR' " +
|
||||
"END")
|
||||
ChannelService.InvalidChannelCreationReason validateChanneUpdate(long projectId, String oldChannelName, @Define String channelName, @Define long colorValue);
|
||||
|
||||
@SqlQuery("SELECT * FROM project_channels WHERE project_id = :projectId AND (name = :channelName OR id = :channelId)")
|
||||
ProjectChannelsTable getProjectChannel(long projectId, String channelName, Long channelId);
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ public class ChannelFactory {
|
||||
if (!hangarConfig.channels.isValidChannelName(channelName)) {
|
||||
throw new HangarException("error.channel.invalidName", channelName);
|
||||
}
|
||||
|
||||
ProjectChannelsTable channel = new ProjectChannelsTable(channelName, color, projectId);
|
||||
channelDao.get().insert(channel);
|
||||
return channel;
|
||||
|
@ -7,6 +7,7 @@ import io.papermc.hangar.db.model.ProjectChannelsTable;
|
||||
import io.papermc.hangar.db.model.ProjectsTable;
|
||||
import io.papermc.hangar.model.Color;
|
||||
import io.papermc.hangar.util.HangarException;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@ -44,6 +45,21 @@ public class ChannelService {
|
||||
|
||||
public ProjectChannelsTable addProjectChannel(long projectId, String channelName, Color color) {
|
||||
InvalidChannelCreationReason reason = channelDao.get().validateChannelCreation(projectId, channelName, color.getValue(), hangarConfig.projects.getMaxChannels());
|
||||
checkInvalidChannelCreationReason(reason);
|
||||
return channelFactory.createChannel(projectId, channelName, color);
|
||||
}
|
||||
|
||||
public void updateProjectChannel(long projectId, String oldChannel, String channelName, Color color) {
|
||||
if (!hangarConfig.channels.isValidChannelName(channelName)) {
|
||||
throw new HangarException("error.channel.invalidName", channelName);
|
||||
}
|
||||
|
||||
InvalidChannelCreationReason reason = channelDao.get().validateChanneUpdate(projectId, oldChannel, channelName, color.getValue());
|
||||
checkInvalidChannelCreationReason(reason);
|
||||
channelDao.get().update(projectId, oldChannel, channelName, color);
|
||||
}
|
||||
|
||||
private void checkInvalidChannelCreationReason(@Nullable InvalidChannelCreationReason reason) {
|
||||
if (reason != null) {
|
||||
if (reason == InvalidChannelCreationReason.MAX_CHANNELS) {
|
||||
throw new HangarException(reason.reason, String.valueOf(hangarConfig.projects.getMaxChannels()));
|
||||
@ -51,8 +67,6 @@ public class ChannelService {
|
||||
throw new HangarException(reason.reason);
|
||||
}
|
||||
}
|
||||
|
||||
return channelFactory.createChannel(projectId, channelName, color);
|
||||
}
|
||||
|
||||
public enum InvalidChannelCreationReason {
|
||||
|
Loading…
Reference in New Issue
Block a user