Skip to content

Commit

Permalink
Created guild database entity
Browse files Browse the repository at this point in the history
Added ControllerAdviser to add shared layout settings to every page
Moved explicit pre controller authorization to annotation when possible
Created DiscordIdEntity (Abstract object and repository) for user and guild
  • Loading branch information
Alf-Melmac committed Jan 7, 2022
1 parent c5e0fe0 commit 9645529
Show file tree
Hide file tree
Showing 50 changed files with 724 additions and 474 deletions.
1 change: 1 addition & 0 deletions .idea/dictionaries/Alf.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions .idea/jpa-buddy.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 18 additions & 10 deletions src/main/java/de/webalf/slotbot/assembler/EventAssembler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,34 @@
import de.webalf.slotbot.model.Event;
import de.webalf.slotbot.model.dtos.EventDto;
import de.webalf.slotbot.model.dtos.referenceless.EventReferencelessDto;
import de.webalf.slotbot.service.GuildService;
import de.webalf.slotbot.util.LongUtils;
import lombok.experimental.UtilityClass;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

import static de.webalf.slotbot.model.Guild.GUILD_PLACEHOLDER;
import static de.webalf.slotbot.util.BooleanUtils.parseBoolean;
import static de.webalf.slotbot.util.GuildUtils.GUILD_PLACEHOLDER;
import static de.webalf.slotbot.util.StringUtils.trim;

/**
* @author Alf
* @since 23.06.2020
*/
@UtilityClass
public final class EventAssembler {
@Component
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class EventAssembler {
private final EventDiscordInformationAssembler discordInformationAssembler;
private final GuildService guildService;

public static Event fromDto(EventDto dto) {
public Event fromDto(EventDto dto) {
if (dto == null) {
return null;
}

return Event.builder()
final Event event = Event.builder()
.id(dto.getId())
.hidden(parseBoolean(dto.getHidden(), false))
.shareable(parseBoolean(dto.getShareable(), false))
Expand All @@ -39,9 +45,11 @@ public static Event fromDto(EventDto dto) {
.details(EventFieldAssembler.fromDtoIterable(dto.getDetails()))
.squadList(SquadAssembler.fromDtoList(dto.getSquadList()))
.reserveParticipating(dto.getReserveParticipating())
.discordInformation(EventDiscordInformationAssembler.fromDtoIterable(dto.getDiscordInformation()))
.ownerGuild(LongUtils.parseLong(dto.getOwnerGuild(), GUILD_PLACEHOLDER))
.discordInformation(discordInformationAssembler.fromDtoIterable(dto.getDiscordInformation()))
.ownerGuild(guildService.find(LongUtils.parseLong(dto.getOwnerGuild(), GUILD_PLACEHOLDER)))
.build();
event.setChilds();
return event;
}

/**
Expand All @@ -66,7 +74,7 @@ public static EventReferencelessDto toReferencelessDto(Event event) {
.squadList(SquadAssembler.toReferencelessDtoList(event.getSquadList()))
.reserveParticipating(event.getReserveParticipating())
.discordInformation(EventDiscordInformationAssembler.toDtoSet(event.getDiscordInformation()))
.ownerGuild(Long.toString(event.getOwnerGuild()))
.ownerGuild(Long.toString(event.getOwnerGuild().getId()))
.build();
}

Expand All @@ -90,7 +98,7 @@ static EventDto toAbstractDto(Event event) {
.pictureUrl(event.getPictureUrl())
.reserveParticipating(event.getReserveParticipating())
.discordInformation(EventDiscordInformationAssembler.toDtoSet(event.getDiscordInformation()))
.ownerGuild(Long.toString(event.getOwnerGuild()))
.ownerGuild(Long.toString(event.getOwnerGuild().getId()))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import de.webalf.slotbot.model.EventDiscordInformation;
import de.webalf.slotbot.model.dtos.EventDiscordInformationDto;
import de.webalf.slotbot.service.GuildService;
import de.webalf.slotbot.util.LongUtils;
import lombok.NonNull;
import lombok.experimental.UtilityClass;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Set;
Expand All @@ -15,29 +18,32 @@
* @author Alf
* @since 04.07.2021
*/
@UtilityClass
public final class EventDiscordInformationAssembler {
public static EventDiscordInformation fromDto(EventDiscordInformationDto dto) {
@Component
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class EventDiscordInformationAssembler {
private final GuildService guildService;

public EventDiscordInformation fromDto(EventDiscordInformationDto dto) {
if (dto == null) {
return null;
}

return EventDiscordInformation.builder()
.channel(Long.parseLong(dto.getChannel()))
.guild(Long.parseLong(dto.getGuild()))
.guild(guildService.find(Long.parseLong(dto.getGuild())))
.infoMsg(LongUtils.parseLongWrapper(dto.getInfoMsg()))
.slotListMsgPartOne(LongUtils.parseLongWrapper(dto.getSlotListMsgPartOne()))
.slotListMsgPartTwo(LongUtils.parseLongWrapper(dto.getSlotListMsgPartTwo()))
.build();
}

static Set<EventDiscordInformation> fromDtoIterable(Iterable<? extends EventDiscordInformationDto> dtos) {
Set<EventDiscordInformation> fromDtoIterable(Iterable<? extends EventDiscordInformationDto> dtos) {
if (dtos == null) {
return Collections.emptySet();
}

return StreamSupport.stream(dtos.spliterator(), true)
.map(EventDiscordInformationAssembler::fromDto)
.map(this::fromDto)
.collect(Collectors.toUnmodifiableSet());
}

Expand All @@ -48,7 +54,7 @@ private static EventDiscordInformationDto toDto(EventDiscordInformation discordI

return EventDiscordInformationDto.builder()
.channel(LongUtils.toString(discordInformation.getChannel()))
.guild(LongUtils.toString(discordInformation.getGuild()))
.guild(LongUtils.toString(discordInformation.getGuild().getId()))
.infoMsg(LongUtils.toString(discordInformation.getInfoMsg()))
.slotListMsgPartOne(LongUtils.toString(discordInformation.getSlotListMsgPartOne()))
.slotListMsgPartTwo(LongUtils.toString(discordInformation.getSlotListMsgPartTwo()))
Expand All @@ -62,7 +68,7 @@ static Set<EventDiscordInformationDto> toDtoSet(Iterable<? extends EventDiscordI
.collect(Collectors.toUnmodifiableSet());
}

private String getChannelUrl(@NonNull EventDiscordInformation discordInformation) {
private static String getChannelUrl(@NonNull EventDiscordInformation discordInformation) {
return "discord://discordapp.com/channels/" + discordInformation.getGuild() + "/" + discordInformation.getChannel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ private static EventField fromDto(EventFieldDto dto) {
.id(dto.getId())
.title(dto.getTitle().trim())
.text(dto.getText().trim())
.event(EventAssembler.fromDto(dto.getEvent()))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ static Squad fromDto(SquadDto squadDto) {
.id(squadDto.getId())
.name(squadDto.getName().trim())
.slotList(SlotAssembler.fromDtoList(squadDto.getSlotList()))
.event(EventAssembler.fromDto(squadDto.getEvent()))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static EventApiDto toDto(@NonNull Event event) {
.details(EventFieldApiAssembler.toDtoList(event.getDetails()))
.squadList(SquadApiAssembler.toDtoList(event.getSquadList()))
.reserveParticipating(event.getReserveParticipating())
.ownerGuild(Long.toString(event.getOwnerGuild()))
.ownerGuild(Long.toString(event.getOwnerGuild().getId()))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import de.webalf.slotbot.assembler.EventFieldAssembler;
import de.webalf.slotbot.assembler.EventTypeAssembler;
import de.webalf.slotbot.model.Event;
import de.webalf.slotbot.model.EventField;
import de.webalf.slotbot.model.Slot;
import de.webalf.slotbot.model.Squad;
import de.webalf.slotbot.model.*;
import de.webalf.slotbot.model.dtos.referenceless.EventFieldReferencelessDto;
import de.webalf.slotbot.model.dtos.website.EventDetailsDto;
import de.webalf.slotbot.model.dtos.website.EventDetailsSlotDto;
Expand Down Expand Up @@ -53,7 +50,7 @@ public EventDetailsDto toDto(@NonNull Event event) {
.details(getDetails(event.getDetails()))
.squadList(toEventDetailsDtoList(event.getSquadList()))
.reserveParticipating(event.getReserveParticipating())
.ownerGuild(Long.toString(event.getOwnerGuild()))
.ownerGuild(Long.toString(event.getOwnerGuild().getId()))
.build();
}

Expand All @@ -76,14 +73,14 @@ public EventEditDto toEditDto(@NonNull Event event) {
.details(EventFieldAssembler.toDefaultDtoList(event.getDetails()))
.squadList(toEventDetailsDtoList(event.getSquadList()))
.reserveParticipating(event.getReserveParticipating())
.ownerGuild(Long.toString(event.getOwnerGuild()))
.ownerGuild(Long.toString(event.getOwnerGuild().getId()))
.build();
}

private String getChannelUrl(@NonNull Event event) {
final long ownerGuild = event.getOwnerGuild();
final Guild ownerGuild = event.getOwnerGuild();
return event.getDiscordInformation(ownerGuild)
.map(eventDiscordInformation -> "discord://discordapp.com/channels/" + ownerGuild + "/" + LongUtils.toString(eventDiscordInformation.getChannel()))
.map(eventDiscordInformation -> "discord://discordapp.com/channels/" + ownerGuild.getId() + "/" + LongUtils.toString(eventDiscordInformation.getChannel()))
.orElse(null);
}

Expand Down Expand Up @@ -133,7 +130,7 @@ private EventDetailsSlotDto toEventDetailsSlotDto(@NonNull Slot slot) {
}
blocked = true;
} else {
text = discordApiService.getName(LongUtils.toString(slot.getUser().getId()), slot.getSquad().getEvent().getOwnerGuild());
text = discordApiService.getName(LongUtils.toString(slot.getUser().getId()), slot.getSquad().getEvent().getOwnerGuild().getId());
}
}

Expand Down
17 changes: 8 additions & 9 deletions src/main/java/de/webalf/slotbot/controller/EventController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import de.webalf.slotbot.model.dtos.referenceless.EventReferencelessDto;
import de.webalf.slotbot.model.dtos.website.CalendarEventDto;
import de.webalf.slotbot.service.EventService;
import de.webalf.slotbot.util.GuildUtils;
import de.webalf.slotbot.util.LongUtils;
import de.webalf.slotbot.service.GuildService;
import de.webalf.slotbot.util.permissions.PermissionChecker;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -23,7 +23,6 @@

import static de.webalf.slotbot.util.eventfield.EventFieldUtils.getDefault;
import static de.webalf.slotbot.util.permissions.ApplicationPermissionHelper.HAS_POTENTIALLY_ROLE_EVENT_MANAGE;
import static de.webalf.slotbot.util.permissions.PermissionHelper.assertEventManagePermission;

/**
* @author Alf
Expand All @@ -34,26 +33,26 @@
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Slf4j
public class EventController {
private final PermissionChecker permissionChecker;
private final GuildService guildService;
private final EventService eventService;

@GetMapping(value = "/list")
public List<CalendarEventDto> getBetween(@RequestParam LocalDateTime start,
@RequestParam LocalDateTime end) {
return CalendarEventAssembler.toDtoList(eventService.findAllBetweenOfGuild(start, end, GuildUtils.getCurrentOwnerGuild()));
return CalendarEventAssembler.toDtoList(eventService.findAllBetweenOfGuild(start, end, guildService.findCurrentNonNullGuild()));
}

@PostMapping
@PreAuthorize(HAS_POTENTIALLY_ROLE_EVENT_MANAGE)
@PreAuthorize("@permissionChecker.assertEventManagePermission(#event.getOwnerGuild())")
public EventReferencelessDto postEvent(@Valid @RequestBody EventDto event) {
assertEventManagePermission(LongUtils.parseLongWrapper(event.getOwnerGuild()));

return EventAssembler.toReferencelessDto(eventService.createEvent(event));
}

@PutMapping("/{id}")
@PreAuthorize(HAS_POTENTIALLY_ROLE_EVENT_MANAGE)
public EventReferencelessDto updateEvent(@PathVariable(value = "id") long eventId, @RequestBody EventDto event) {
assertEventManagePermission(eventService.getGuildIdByEventId(eventId));
permissionChecker.assertEventManagePermission(eventService.getGuildByEventId(eventId));

event.setId(eventId);
return EventAssembler.toReferencelessDto(eventService.updateEvent(event));
Expand All @@ -62,7 +61,7 @@ public EventReferencelessDto updateEvent(@PathVariable(value = "id") long eventI
@PostMapping("/editable")
@PreAuthorize(HAS_POTENTIALLY_ROLE_EVENT_MANAGE)
public EventReferencelessDto updateEventEditable(long pk, String name, String value) {
assertEventManagePermission(eventService.getGuildIdByEventId(pk));
permissionChecker.assertEventManagePermission(eventService.getGuildByEventId(pk));

EventDto dto = EventDto.builder().id(pk).build();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.webalf.slotbot.model.dtos.EventDto;
import de.webalf.slotbot.model.dtos.api.EventApiDto;
import de.webalf.slotbot.service.EventService;
import de.webalf.slotbot.util.permissions.EventPermissionHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -13,7 +14,6 @@
import javax.validation.Valid;

import static de.webalf.slotbot.constant.Urls.API;
import static de.webalf.slotbot.util.EventUtils.assertApiWriteAccess;
import static de.webalf.slotbot.util.permissions.ApiPermissionHelper.HAS_POTENTIAL_READ_PUBLIC_PERMISSION;
import static de.webalf.slotbot.util.permissions.ApiPermissionHelper.HAS_POTENTIAL_WRITE_PERMISSION;

Expand All @@ -27,6 +27,7 @@
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class EventApiController {
private final EventService eventService;
private final EventPermissionHelper eventPermissionHelper;

@GetMapping("/{id}")
@PreAuthorize(HAS_POTENTIAL_READ_PUBLIC_PERMISSION)
Expand All @@ -36,10 +37,9 @@ public EventApiDto getEvent(@PathVariable(value = "id") long eventId) {
}

@PostMapping
@PreAuthorize(HAS_POTENTIAL_WRITE_PERMISSION)
@PreAuthorize("@eventPermissionHelper.assertApiWriteAccess(#event)")
public EventApiDto postEvent(@Valid @RequestBody EventDto event) {
log.trace("postEvent: " + event.getName());
assertApiWriteAccess(event);
return EventApiAssembler.toDto(eventService.createEvent(event));
}

Expand All @@ -48,7 +48,7 @@ public EventApiDto postEvent(@Valid @RequestBody EventDto event) {
public EventApiDto updateEvent(@PathVariable(value = "id") long eventId, @RequestBody EventDto event) {
log.trace("updateEvent: " + event.getName());
event.setId(eventId);
assertApiWriteAccess(eventService.findById(eventId));
eventPermissionHelper.assertApiWriteAccess(eventService.findById(eventId));
return EventApiAssembler.toDto(eventService.updateEvent(event));
}
}

0 comments on commit 9645529

Please sign in to comment.