Channel saving

This commit is contained in:
KennyTV 2020-08-22 11:03:07 +02:00
parent 25cf62e383
commit 710030a5e6
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
4 changed files with 30 additions and 6 deletions

View File

@ -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));
}

View File

@ -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);
}

View File

@ -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;

View File

@ -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 {