Skip to content

Commit

Permalink
Fixed event type not creatable
Browse files Browse the repository at this point in the history
  • Loading branch information
Alf-Melmac committed Jul 28, 2021
1 parent 7b96ab7 commit 6794ebb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
11 changes: 3 additions & 8 deletions src/main/java/de/webalf/slotbot/model/EventType.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,19 @@ public class EventType extends AbstractSuperIdEntity {

@Column(name = "event_color", length = 7)
@NotBlank
@Size(max = 7) //Expected format: #rrggbb
@Size(max = 7) //Expected format: #RRGGBB
private String color;

@OneToMany(mappedBy = "eventType")
private List<Event> events;

@Override
public long getId() throws IllegalCallerException {
throw new IllegalCallerException("EventTypes should be identified by their name and color pair.");
}

public void setColor(String color) {
if (!HEX_COLOR.matcher(color).matches()) {
throw BusinessRuntimeException.builder().title(color + " is not a valid hex color.").build();
throw BusinessRuntimeException.builder().title(color + " is not a valid upper case hex color.").build();
}

this.color = color.startsWith("#") ? color : "#" + color;
}

private static final Pattern HEX_COLOR = Pattern.compile("^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$");
private static final Pattern HEX_COLOR = Pattern.compile("^#?([A-F0-9]{6}|[A-F0-9]{3})$");
}
4 changes: 4 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 @@ -28,4 +28,8 @@ public class EventTypeDto extends AbstractIdEntityDto {
@NotBlank
@Size(max = 7)
String color;

public String getColor() {
return color.toUpperCase();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package de.webalf.slotbot.repository;

import de.webalf.slotbot.model.EventType;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import javax.transaction.NotSupportedException;
import java.util.List;
import java.util.Optional;

/**
Expand All @@ -13,4 +17,19 @@
@Repository
public interface EventTypeRepository extends JpaRepository<EventType, Long> {
Optional<EventType> findEventTypeByNameAndColor(String name, String color);

/**
* @see #findEventTypeByNameAndColor(String, String)
*/
@SneakyThrows(NotSupportedException.class)
@Override
default Optional<EventType> findById(@NotNull Long l) {
throw new NotSupportedException("Id shouldn't be used to get entity. Use findEventTypeByNameAndColor");
}

@SneakyThrows(NotSupportedException.class)
@Override
default List<EventType> findAllById(@NotNull Iterable<Long> iterable) {
throw new NotSupportedException("Id shouldn't be used to get entity.");
}
}

0 comments on commit 6794ebb

Please sign in to comment.