Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ch.wisv.chpay.admin.service.ConfettiFormParser;
import ch.wisv.chpay.admin.service.ConfettiFormParser.ConfettiFormResult;
import ch.wisv.chpay.core.model.Confetti;
import ch.wisv.chpay.core.model.LedPattern;
import ch.wisv.chpay.core.service.NotificationService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -38,7 +39,7 @@ public AdminConfettiCreateController(
@GetMapping
public String showCreateForm(Model model) {
model.addAttribute("confettiName", "");
model.addAttribute("colorsCsv", "");
model.addAttribute("colorValues", List.of("#ff0000"));
model.addAttribute("scalarValue", Confetti.DEFAULT_SCALAR);
model.addAttribute("minTransactionsValue", 0);
model.addAttribute("groupValue", "");
Expand All @@ -47,14 +48,16 @@ public String showCreateForm(Model model) {
model.addAttribute("defaultValue", false);
model.addAttribute("shapeTypes", List.of("SQUARE"));
model.addAttribute("shapeValues", List.of(""));
model.addAttribute("ledColorValue", "#ffffff");
model.addAttribute("ledPatternValue", LedPattern.oplopen.name());
model.addAttribute(MODEL_ATTR_URL_PAGE, "adminConfetti");
return "admin-confetti-create";
}

@PostMapping
public String createConfetti(
@RequestParam("name") String name,
@RequestParam("colors") String colorsInput,
@RequestParam(value = "colors", required = false) List<String> colorsInput,
@RequestParam("scalar") String scalarInput,
@RequestParam("minTransactions") String minTransactionsInput,
@RequestParam("group") String groupInput,
Expand All @@ -63,6 +66,8 @@ public String createConfetti(
@RequestParam(value = "isDefault", defaultValue = "false") boolean isDefault,
@RequestParam(value = "shapeType", required = false) List<String> shapeTypes,
@RequestParam(value = "shapeValue", required = false) List<String> shapeValues,
@RequestParam("ledColor") String ledColor,
@RequestParam("ledPattern") String ledPattern,
RedirectAttributes redirectAttributes) {

ConfettiFormResult result =
Expand All @@ -76,7 +81,9 @@ public String createConfetti(
hidden,
isDefault,
shapeTypes,
shapeValues);
shapeValues,
ledColor,
ledPattern);
if (!result.isValid()) {
notificationService.addErrorMessage(redirectAttributes, result.getErrorMessage());
return "redirect:/admin/confetti/new";
Expand All @@ -92,7 +99,9 @@ public String createConfetti(
result.getGroup(),
result.isGroupStartsWith(),
result.isHidden(),
result.isDefault());
result.isDefault(),
result.getLedColor(),
result.getLedPattern());
notificationService.addSuccessMessage(redirectAttributes, "Confetti created successfully");
return "redirect:/admin/confetti/" + confetti.getId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public String showConfetti(@PathVariable("id") UUID id, Model model, RedirectAtt
Confetti current = confetti.get();
model.addAttribute(MODEL_ATTR_CONFETTI, current);
model.addAttribute("confettiUserCount", adminConfettiService.getUsageCount(current));
model.addAttribute("colorsCsv", String.join(", ", current.getColors()));
model.addAttribute("colorValues", current.getColors());
model.addAttribute("scalarValue", current.getScalar());
model.addAttribute("minTransactionsValue", current.getMinimumTransactions());
model.addAttribute("groupValue", current.getGroup());
Expand All @@ -65,6 +65,8 @@ public String showConfetti(@PathVariable("id") UUID id, Model model, RedirectAtt
.map(shape -> shape.getValue() == null ? "" : shape.getValue().trim())
.toList());

model.addAttribute("ledColorValue", current.getLedColor());
model.addAttribute("ledPatternValue", current.getLedPattern().name());
model.addAttribute(MODEL_ATTR_URL_PAGE, "adminConfetti");
return "admin-confetti";
}
Expand All @@ -73,7 +75,7 @@ public String showConfetti(@PathVariable("id") UUID id, Model model, RedirectAtt
public String updateConfetti(
@PathVariable("id") UUID id,
@RequestParam("name") String name,
@RequestParam("colors") String colorsInput,
@RequestParam(value = "colors", required = false) List<String> colorsInput,
@RequestParam("scalar") String scalarInput,
@RequestParam("minTransactions") String minTransactionsInput,
@RequestParam("group") String groupInput,
Expand All @@ -82,6 +84,8 @@ public String updateConfetti(
@RequestParam(value = "isDefault", defaultValue = "false") boolean isDefault,
@RequestParam(value = "shapeType", required = false) List<String> shapeTypes,
@RequestParam(value = "shapeValue", required = false) List<String> shapeValues,
@RequestParam("ledColor") String ledColor,
@RequestParam("ledPattern") String ledPattern,
RedirectAttributes redirectAttributes) {

Optional<Confetti> confetti = adminConfettiService.getById(id);
Expand All @@ -101,7 +105,9 @@ public String updateConfetti(
hidden,
isDefault,
shapeTypes,
shapeValues);
shapeValues,
ledColor,
ledPattern);
if (!result.isValid()) {
notificationService.addErrorMessage(redirectAttributes, result.getErrorMessage());
return "redirect:/admin/confetti/" + id;
Expand All @@ -118,7 +124,9 @@ public String updateConfetti(
result.getGroup(),
result.isGroupStartsWith(),
result.isHidden(),
result.isDefault());
result.isDefault(),
result.getLedColor(),
result.getLedPattern());
} catch (IllegalStateException ex) {
notificationService.addErrorMessage(redirectAttributes, ex.getMessage());
return "redirect:/admin/confetti/" + id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ch.wisv.chpay.core.model.Confetti;
import ch.wisv.chpay.core.model.ConfettiShape;
import ch.wisv.chpay.core.model.LedPattern;
import ch.wisv.chpay.core.repository.ConfettiRepository;
import ch.wisv.chpay.core.repository.ConfettiUsageCount;
import ch.wisv.chpay.core.repository.UserRepository;
Expand Down Expand Up @@ -78,7 +79,9 @@ public Confetti create(
String group,
boolean groupStartsWith,
boolean hidden,
boolean isDefault) {
boolean isDefault,
String ledColor,
LedPattern ledPattern) {
boolean shouldBeDefault = isDefault || confettiRepository.countByDefaultConfettiTrue() == 0;
Confetti confetti =
new Confetti(
Expand All @@ -90,7 +93,9 @@ public Confetti create(
group,
groupStartsWith,
hidden,
shouldBeDefault);
shouldBeDefault,
ledColor,
ledPattern);
Confetti saved = confettiRepository.save(confetti);
enforceSingleDefault(saved.getId());
return saved;
Expand All @@ -108,7 +113,9 @@ public Confetti update(
String group,
boolean groupStartsWith,
boolean hidden,
boolean isDefault) {
boolean isDefault,
String ledColor,
LedPattern ledPattern) {
if (!isDefault
&& confetti.isDefaultConfetti()
&& confettiRepository.countByDefaultConfettiTrue() == 1) {
Expand All @@ -123,7 +130,9 @@ public Confetti update(
group,
groupStartsWith,
hidden,
isDefault);
isDefault,
ledColor,
ledPattern);
Confetti saved = confettiRepository.save(confetti);
enforceSingleDefault(saved.getId());
return saved;
Expand Down
68 changes: 51 additions & 17 deletions src/main/java/ch/wisv/chpay/admin/service/ConfettiFormParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import ch.wisv.chpay.core.model.ConfettiShape;
import ch.wisv.chpay.core.model.ConfettiShapeType;
import ch.wisv.chpay.core.model.LedPattern;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
Expand All @@ -15,22 +15,30 @@ public class ConfettiFormParser {

public ConfettiFormResult parse(
String name,
String colorsInput,
List<String> colorsInput,
String scalarInput,
String minimumTransactionsInput,
String groupInput,
boolean groupStartsWith,
boolean hidden,
boolean isDefault,
List<String> shapeTypes,
List<String> shapeValues) {
List<String> shapeValues,
String ledColorInput,
String ledPatternInput) {

String trimmedName = name == null ? "" : name.trim();
if (trimmedName.isEmpty()) {
return ConfettiFormResult.error("Name is required");
}

List<String> colors = parseColors(colorsInput);
List<String> colors =
colorsInput == null
? List.of()
: colorsInput.stream()
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(Collectors.toList());
if (colors.isEmpty() || colors.stream().anyMatch(color -> !isValidColor(color))) {
return ConfettiFormResult.error("Please provide at least one valid hex color (e.g. #FF0000)");
}
Expand All @@ -57,6 +65,18 @@ public ConfettiFormResult parse(
return ConfettiFormResult.error("Add at least one shape");
}

String ledColor = ledColorInput == null ? "" : ledColorInput.trim();
if (!isValidLedColor(ledColor)) {
return ConfettiFormResult.error("Please provide a valid LED color (e.g. #FF0000)");
}

LedPattern ledPattern;
try {
ledPattern = LedPattern.valueOf(ledPatternInput == null ? "" : ledPatternInput.trim());
} catch (IllegalArgumentException e) {
return ConfettiFormResult.error("Please select a valid LED pattern");
}

return ConfettiFormResult.success(
trimmedName,
colors,
Expand All @@ -66,23 +86,19 @@ public ConfettiFormResult parse(
normalizeGroup(groupInput),
normalizeGroupStartsWith(groupStartsWith, groupInput),
hidden,
isDefault);
}

private List<String> parseColors(String colorsInput) {
if (colorsInput == null) {
return List.of();
}
return Arrays.stream(colorsInput.split(","))
.map(String::trim)
.filter(value -> !value.isEmpty())
.collect(Collectors.toList());
isDefault,
ledColor,
ledPattern);
}

private boolean isValidColor(String color) {
return color.matches("^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$");
}

private boolean isValidLedColor(String color) {
return color.matches("^#[0-9a-fA-F]{6}$");
}

private ScalarParseResult parseScalar(String scalarInput) {
if (scalarInput == null || scalarInput.trim().isEmpty()) {
return ScalarParseResult.invalid();
Expand Down Expand Up @@ -197,6 +213,8 @@ public static final class ConfettiFormResult {
private final boolean groupStartsWith;
private final boolean hidden;
private final boolean isDefault;
private final String ledColor;
private final LedPattern ledPattern;
private final String errorMessage;

private ConfettiFormResult(
Expand All @@ -209,6 +227,8 @@ private ConfettiFormResult(
boolean groupStartsWith,
boolean hidden,
boolean isDefault,
String ledColor,
LedPattern ledPattern,
String errorMessage) {
this.name = name;
this.colors = colors;
Expand All @@ -219,6 +239,8 @@ private ConfettiFormResult(
this.groupStartsWith = groupStartsWith;
this.hidden = hidden;
this.isDefault = isDefault;
this.ledColor = ledColor;
this.ledPattern = ledPattern;
this.errorMessage = errorMessage;
}

Expand All @@ -231,7 +253,9 @@ public static ConfettiFormResult success(
String group,
boolean groupStartsWith,
boolean hidden,
boolean isDefault) {
boolean isDefault,
String ledColor,
LedPattern ledPattern) {
return new ConfettiFormResult(
name,
colors,
Expand All @@ -242,12 +266,14 @@ public static ConfettiFormResult success(
groupStartsWith,
hidden,
isDefault,
ledColor,
ledPattern,
null);
}

public static ConfettiFormResult error(String message) {
return new ConfettiFormResult(
null, List.of(), List.of(), 0.0, 0, null, false, false, false, message);
null, List.of(), List.of(), 0.0, 0, null, false, false, false, null, null, message);
}

public boolean isValid() {
Expand Down Expand Up @@ -290,6 +316,14 @@ public boolean isDefault() {
return isDefault;
}

public String getLedColor() {
return ledColor;
}

public LedPattern getLedPattern() {
return ledPattern;
}

public String getErrorMessage() {
return errorMessage;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.wisv.chpay.api.ledstrip.controller;

import ch.wisv.chpay.core.model.Confetti;
import ch.wisv.chpay.core.model.LedPattern;
import ch.wisv.chpay.core.model.User;
import ch.wisv.chpay.core.model.transaction.Transaction;
Expand Down Expand Up @@ -53,10 +54,15 @@ public ResponseEntity<LatestTransactionResponse> getLatestTransaction() {
.map(
t -> {
User user = t.getUser();
Integer r = user != null ? user.getLedR() : null;
Integer g = user != null ? user.getLedG() : null;
Integer b = user != null ? user.getLedB() : null;
LedPattern ledPattern = user != null ? user.getLedPattern() : null;
Confetti confetti = user != null ? user.getConfetti() : null;
String ledColor = confetti != null ? confetti.getLedColor() : null;
LedPattern ledPattern = confetti != null ? confetti.getLedPattern() : null;
Integer r = null, g = null, b = null;
if (ledColor != null && ledColor.length() == 7) {
r = Integer.parseInt(ledColor.substring(1, 3), 16);
g = Integer.parseInt(ledColor.substring(3, 5), 16);
b = Integer.parseInt(ledColor.substring(5, 7), 16);
}
String pattern = ledPattern != null ? ledPattern.name() : null;
return ResponseEntity.ok(
new LatestTransactionResponse(
Expand Down
Loading
Loading