Add and get riddles!
This commit is contained in:
1 parent
1a41d26599
commit
11c47898c3
11 files changed
+332
-2
No files matched your search
@@ -77,6 +77,18 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- Source: https://mvnrepository.com/artifact/org.mapstruct/mapstruct -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mapstruct</groupId>
|
||||||
|
<artifactId>mapstruct</artifactId>
|
||||||
|
<version>1.6.3</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok-mapstruct-binding</artifactId>
|
||||||
|
<version>0.2.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@@ -97,10 +109,20 @@
|
|||||||
</goals>
|
</goals>
|
||||||
<configuration>
|
<configuration>
|
||||||
<annotationProcessorPaths>
|
<annotationProcessorPaths>
|
||||||
|
<path>
|
||||||
|
<groupId>org.mapstruct</groupId>
|
||||||
|
<artifactId>mapstruct-processor</artifactId>
|
||||||
|
<version>1.6.3</version>
|
||||||
|
</path>
|
||||||
<path>
|
<path>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
</path>
|
</path>
|
||||||
|
<path>
|
||||||
|
<groupId>org.mapstruct</groupId>
|
||||||
|
<artifactId>mapstruct-processor</artifactId>
|
||||||
|
<version>1.6.3</version>
|
||||||
|
</path>
|
||||||
</annotationProcessorPaths>
|
</annotationProcessorPaths>
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
|
|||||||
@@ -1,10 +1,22 @@
|
|||||||
package de.exodus_hn.excalibur_tour.controller;
|
package de.exodus_hn.excalibur_tour.controller;
|
||||||
|
|
||||||
|
import de.exodus_hn.excalibur_tour.Utils;
|
||||||
|
import de.exodus_hn.excalibur_tour.dto.riddle.CreateRiddleDTO;
|
||||||
|
import de.exodus_hn.excalibur_tour.dto.riddle.ReadAllRiddleData;
|
||||||
|
import de.exodus_hn.excalibur_tour.dto.riddle.ReadRiddleData;
|
||||||
|
import de.exodus_hn.excalibur_tour.entities.Riddle;
|
||||||
import de.exodus_hn.excalibur_tour.entities.TourCode;
|
import de.exodus_hn.excalibur_tour.entities.TourCode;
|
||||||
|
import de.exodus_hn.excalibur_tour.mapper.RiddleMapper;
|
||||||
|
import de.exodus_hn.excalibur_tour.services.RiddleService;
|
||||||
import de.exodus_hn.excalibur_tour.services.TourCodeService;
|
import de.exodus_hn.excalibur_tour.services.TourCodeService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
|
|
||||||
@CrossOrigin
|
@CrossOrigin
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/codes")
|
@RequestMapping("/codes")
|
||||||
@@ -20,7 +32,24 @@ public class CodeController {
|
|||||||
t = tourCodeService.addTourCode(code);
|
t = tourCodeService.addTourCode(code);
|
||||||
return "Created TC " + t.getId();
|
return "Created TC " + t.getId();
|
||||||
}
|
}
|
||||||
return "Hello " + t.getCode() + "(" + t.getActivated() + ") " + t.getCreationTimestamp();
|
LocalDateTime now = Utils.getDateTimeNow();
|
||||||
|
if (t.getActivated()) {
|
||||||
|
LocalDateTime activationTimestamp = t.getActivationTimestamp();
|
||||||
|
if (activationTimestamp == null) {
|
||||||
|
t.setActivationTimestamp(now);
|
||||||
|
tourCodeService.updateCode(t);
|
||||||
|
} else if (activationTimestamp.plus(10, ChronoUnit.SECONDS).isBefore(now)) {
|
||||||
|
return "Expired";
|
||||||
|
}
|
||||||
|
LocalDateTime heartbeat = t.getLastHeartbeatTimestamp();
|
||||||
|
if (heartbeat != null && heartbeat.plus(2, ChronoUnit.SECONDS).isAfter(now)) {
|
||||||
|
return "Expired (Wait a bit please)";
|
||||||
|
}
|
||||||
|
t.setLastHeartbeatTimestamp(now);
|
||||||
|
tourCodeService.updateCode(t);
|
||||||
|
return "Have fun Ig";
|
||||||
|
}
|
||||||
|
return "Expired (Disabled actually";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
|
||||||
|
|
||||||
|
package de.exodus_hn.excalibur_tour.controller;
|
||||||
|
|
||||||
|
import de.exodus_hn.excalibur_tour.dto.riddle.CreateRiddleDTO;
|
||||||
|
import de.exodus_hn.excalibur_tour.dto.riddle.ReadAllRiddleData;
|
||||||
|
import de.exodus_hn.excalibur_tour.dto.riddle.ReadRiddleData;
|
||||||
|
import de.exodus_hn.excalibur_tour.entities.Riddle;
|
||||||
|
import de.exodus_hn.excalibur_tour.mapper.RiddleMapper;
|
||||||
|
import de.exodus_hn.excalibur_tour.services.RiddleService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@CrossOrigin
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/riddles")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class RiddleController {
|
||||||
|
|
||||||
|
private final RiddleService riddleService;
|
||||||
|
private final RiddleMapper riddleMapper;
|
||||||
|
|
||||||
|
@GetMapping(name = "Get all riddles")
|
||||||
|
public ResponseEntity<ReadAllRiddleData> getAllRiddles() {
|
||||||
|
return ResponseEntity.ok(ReadAllRiddleData.builder()
|
||||||
|
.riddles(riddleService.getAllRiddles().stream()
|
||||||
|
.map(riddleMapper::riddleToReadRiddleData)
|
||||||
|
.toList())
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(name = "Create a new riddle")
|
||||||
|
public ResponseEntity<ReadRiddleData> createRiddle(@RequestBody CreateRiddleDTO createDto) {
|
||||||
|
Riddle savedRiddle = riddleService.addRiddle(createDto);
|
||||||
|
return ResponseEntity.status(HttpStatus.CREATED)
|
||||||
|
.body(riddleMapper.riddleToReadRiddleData(savedRiddle));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package de.exodus_hn.excalibur_tour.dto.riddle;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class CreateRiddleDTO {
|
||||||
|
private String name;
|
||||||
|
private Integer step;
|
||||||
|
private Double latitude;
|
||||||
|
private Double longitude;
|
||||||
|
private Integer points;
|
||||||
|
private Integer failPenaltyPoints;
|
||||||
|
private Integer skipPenaltyPoints;
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package de.exodus_hn.excalibur_tour.dto.riddle;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Builder
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public class ReadAllRiddleData {
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@JsonProperty(value = "riddles", required = true)
|
||||||
|
private List<ReadRiddleData> riddles;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package de.exodus_hn.excalibur_tour.dto.riddle;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import de.exodus_hn.excalibur_tour.dto.riddlehint.ReadRiddleHint;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Builder
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public class ReadRiddleData {
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@JsonProperty(value = "id", required = true)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@JsonProperty(value = "name", required = true)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@JsonProperty(value = "step", required = true)
|
||||||
|
private Integer step;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@JsonProperty(value = "latitude", required = true)
|
||||||
|
private Double latitude;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@JsonProperty(value = "longitude", required = true)
|
||||||
|
private Double longitude;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@JsonProperty(value = "points", required = true)
|
||||||
|
private Integer points;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@JsonProperty(value = "failPenaltyPoints", required = true)
|
||||||
|
private Integer failPenaltyPoints;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@JsonProperty(value = "skipPenaltyPoints", required = true)
|
||||||
|
private Integer skipPenaltyPoints;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@JsonProperty(value = "hints", required = true)
|
||||||
|
private List<ReadRiddleHint> hints;
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package de.exodus_hn.excalibur_tour.dto.riddlehint;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import de.exodus_hn.excalibur_tour.entities.RiddleHint;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Builder
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public class ReadRiddleHint {
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@JsonProperty(value = "id", required = true)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@JsonProperty(value = "hint", required = true)
|
||||||
|
private String hint;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@JsonProperty(value = "penaltyPoints", required = true)
|
||||||
|
private Integer penaltyPoints;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@JsonProperty(value = "hintOrder", required = true)
|
||||||
|
private Integer hintOrder;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package de.exodus_hn.excalibur_tour.mapper;
|
||||||
|
|
||||||
|
import de.exodus_hn.excalibur_tour.dto.riddle.CreateRiddleDTO;
|
||||||
|
import de.exodus_hn.excalibur_tour.dto.riddle.ReadRiddleData;
|
||||||
|
import de.exodus_hn.excalibur_tour.dto.riddlehint.ReadRiddleHint;
|
||||||
|
import de.exodus_hn.excalibur_tour.entities.Riddle;
|
||||||
|
import de.exodus_hn.excalibur_tour.entities.RiddleHint;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
|
||||||
|
@Mapper(componentModel = "spring")
|
||||||
|
public interface RiddleMapper {
|
||||||
|
|
||||||
|
default ReadRiddleData riddleToReadRiddleData(Riddle r) {
|
||||||
|
return ReadRiddleData.builder()
|
||||||
|
.id(r.getId())
|
||||||
|
.name(r.getName())
|
||||||
|
.latitude(r.getLatitude())
|
||||||
|
.longitude(r.getLongitude())
|
||||||
|
.points(r.getPoints())
|
||||||
|
.step(r.getStep())
|
||||||
|
.failPenaltyPoints(r.getFailPenaltyPoints())
|
||||||
|
.skipPenaltyPoints(r.getSkipPenaltyPoints())
|
||||||
|
.hints(r.getHints().stream().map(this::riddleHintToReadRiddleHint).toList())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
default Riddle toEntity(CreateRiddleDTO dto) {
|
||||||
|
if (dto == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return Riddle.builder()
|
||||||
|
.name(dto.getName())
|
||||||
|
.step(dto.getStep())
|
||||||
|
.latitude(dto.getLatitude())
|
||||||
|
.longitude(dto.getLongitude())
|
||||||
|
.points(dto.getPoints())
|
||||||
|
.failPenaltyPoints(dto.getFailPenaltyPoints())
|
||||||
|
.skipPenaltyPoints(dto.getSkipPenaltyPoints())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
default ReadRiddleHint riddleHintToReadRiddleHint(RiddleHint riddleHint) {
|
||||||
|
if (riddleHint == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return ReadRiddleHint.builder()
|
||||||
|
.id(riddleHint.getId())
|
||||||
|
.hint(riddleHint.getHint())
|
||||||
|
.penaltyPoints(riddleHint.getPenaltyPoints())
|
||||||
|
.hintOrder(riddleHint.getHintOrder())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package de.exodus_hn.excalibur_tour.repository;
|
||||||
|
|
||||||
|
import de.exodus_hn.excalibur_tour.entities.Riddle;
|
||||||
|
import de.exodus_hn.excalibur_tour.entities.TourCode;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface RiddleRepository extends JpaRepository<Riddle, Long> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package de.exodus_hn.excalibur_tour.services;
|
||||||
|
|
||||||
|
import de.exodus_hn.excalibur_tour.dto.riddle.CreateRiddleDTO;
|
||||||
|
import de.exodus_hn.excalibur_tour.dto.riddle.ReadRiddleData;
|
||||||
|
import de.exodus_hn.excalibur_tour.entities.Riddle;
|
||||||
|
import de.exodus_hn.excalibur_tour.mapper.RiddleMapper;
|
||||||
|
import de.exodus_hn.excalibur_tour.repository.RiddleRepository;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class RiddleService {
|
||||||
|
|
||||||
|
private final RiddleRepository riddleRepository;
|
||||||
|
private final RiddleMapper riddleMapper;
|
||||||
|
|
||||||
|
public @NotNull List<Riddle> getAllRiddles() {
|
||||||
|
return riddleRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Riddle getRiddleById(Long id) {
|
||||||
|
return riddleRepository.getReferenceById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Riddle addRidddle(Riddle riddle) {
|
||||||
|
return riddleRepository.save(riddle);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Riddle updateRiddle(Riddle riddle) {
|
||||||
|
return riddleRepository.save(riddle);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Riddle addRiddle(CreateRiddleDTO createDto) {
|
||||||
|
Riddle riddle = riddleMapper.toEntity(createDto);
|
||||||
|
return riddleRepository.save(riddle);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,7 +23,7 @@ public class TourCodeService {
|
|||||||
return tourCodeRepository.save(new TourCode(code));
|
return tourCodeRepository.save(new TourCode(code));
|
||||||
}
|
}
|
||||||
|
|
||||||
public TourCode updateUser(TourCode tourCode) {
|
public TourCode updateCode(TourCode tourCode) {
|
||||||
return tourCodeRepository.save(tourCode);
|
return tourCodeRepository.save(tourCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user