Skip to content

Commit

Permalink
Endpoint to get event types for given guild
Browse files Browse the repository at this point in the history
Add guild to EventTypeDto
  • Loading branch information
Alf-Melmac committed Feb 13, 2023
1 parent 9c27b57 commit 836f33d
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ public static EventType fromDto(@NonNull EventTypeDto dto, Guild guild) {
}

public static EventTypeDto toDto(EventType eventType) {
final Guild guild = eventType.getGuild();
final String guildId = guild != null ? Long.toString(guild.getId()) : null;
return EventTypeDto.builder()
.name(eventType.getName())
.color(eventType.getColor())
.guild(guildId)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import de.webalf.slotbot.model.dtos.website.guild.GuildConfigDto;
import lombok.experimental.UtilityClass;

import java.util.Collections;

/**
* @author Alf
* @since 12.02.2023
Expand All @@ -15,7 +13,6 @@ public final class GuildConfigAssembler {
public GuildConfigDto toDto(Guild guild) {
return GuildConfigDto.builder()
.language(guild.getLanguage())
.eventTypes(Collections.emptyList()) //TODO add guild specific event types
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import de.webalf.slotbot.assembler.EventTypeAssembler;
import de.webalf.slotbot.model.dtos.EventTypeDto;
import de.webalf.slotbot.service.EventTypeService;
import de.webalf.slotbot.service.GuildService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -16,13 +18,20 @@

@RestController
@RequestMapping("/events/types")
@PreAuthorize(HAS_POTENTIALLY_ROLE_EVENT_MANAGE)
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class EventTypeController {
private final EventTypeService eventTypeService;
private final GuildService guildService;

@GetMapping
@PreAuthorize(HAS_POTENTIALLY_ROLE_EVENT_MANAGE)
public List<EventTypeDto> getEventTypes() {
return EventTypeAssembler.toDtoList(eventTypeService.findAllOrdered());
return EventTypeAssembler.toDtoList(eventTypeService.findAllOrdered(guildService.findCurrentNonNullGuild()));
}

@GetMapping("/{guildId}")
@PreAuthorize("@permissionChecker.hasGuildAdminPrivileges(#guildId)")
public List<EventTypeDto> getEventTypes(@PathVariable(value = "guildId") long guildId) {
return EventTypeAssembler.toDtoList(eventTypeService.findAllOrdered(guildService.findExisting(guildId)));
}
}
3 changes: 3 additions & 0 deletions src/main/java/de/webalf/slotbot/model/dtos/EventTypeDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public class EventTypeDto {
@Size(max = COLOR_RGB)
String color;

//String is needed, because the discord IDs exceed the maximum size of a JavaScript number.
String guild;

public String getColor() {
return color.toLowerCase();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package de.webalf.slotbot.model.dtos.website.guild;

import de.webalf.slotbot.model.dtos.EventTypeDto;
import de.webalf.slotbot.model.enums.Language;
import lombok.Builder;
import lombok.Value;

import java.util.List;

/**
* @author Alf
* @since 12.02.2023
Expand All @@ -15,5 +12,4 @@
@Builder
public class GuildConfigDto {
Language language;
List<EventTypeDto> eventTypes;
}
6 changes: 3 additions & 3 deletions src/main/java/de/webalf/slotbot/service/EventTypeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class EventTypeService {
private final EventTypeRepository eventTypeRepository;
private final GuildService guildService;

/**
* Finds a {@link EventType} by id of given dto or creates a new one with values from given dto for the given {@link Guild}
Expand All @@ -38,10 +37,11 @@ public EventType find(@NonNull EventTypeDto eventTypeDto, Guild guild) {
/**
* Finds all global and {@link GuildService#findCurrentNonNullGuild() guild specific} {@link EventType}s
*
* @param guild to find event types for
* @return event types ordered by name
*/
public List<EventType> findAllOrdered() {
return eventTypeRepository.findByGuildNullOrGuildOrderByName(guildService.findCurrentNonNullGuild());
public List<EventType> findAllOrdered(@NonNull Guild guild) {
return eventTypeRepository.findByGuildNullOrGuildOrderByName(guild);
}

public void deleteUnused() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/webalf/slotbot/service/GuildService.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Guild find(long id) {
* @return Guild found by id
* @throws ResourceNotFoundException if no guild with this guildId could be found
*/
Guild findExisting(long guildId) {
public Guild findExisting(long guildId) {
return guildRepository.findById(guildId).orElseThrow(ResourceNotFoundException::new);
}

Expand Down

0 comments on commit 836f33d

Please sign in to comment.