Add RiddleHints!
This commit is contained in:
1 parent
11c47898c3
commit
b7561ca55c
8 files changed
+88
-18
No files matched your search
@@ -1,17 +1,9 @@
|
|||||||
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.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.LocalDateTime;
|
||||||
|
|||||||
@@ -2,12 +2,15 @@
|
|||||||
|
|
||||||
package de.exodus_hn.excalibur_tour.controller;
|
package de.exodus_hn.excalibur_tour.controller;
|
||||||
|
|
||||||
import de.exodus_hn.excalibur_tour.dto.riddle.CreateRiddleDTO;
|
import de.exodus_hn.excalibur_tour.dto.riddle.CreateRiddle;
|
||||||
import de.exodus_hn.excalibur_tour.dto.riddle.ReadAllRiddleData;
|
import de.exodus_hn.excalibur_tour.dto.riddle.ReadAllRiddleData;
|
||||||
import de.exodus_hn.excalibur_tour.dto.riddle.ReadRiddleData;
|
import de.exodus_hn.excalibur_tour.dto.riddle.ReadRiddleData;
|
||||||
|
import de.exodus_hn.excalibur_tour.dto.riddlehint.CreateRiddleHint;
|
||||||
import de.exodus_hn.excalibur_tour.entities.Riddle;
|
import de.exodus_hn.excalibur_tour.entities.Riddle;
|
||||||
|
import de.exodus_hn.excalibur_tour.entities.RiddleHint;
|
||||||
import de.exodus_hn.excalibur_tour.mapper.RiddleMapper;
|
import de.exodus_hn.excalibur_tour.mapper.RiddleMapper;
|
||||||
import de.exodus_hn.excalibur_tour.services.RiddleService;
|
import de.exodus_hn.excalibur_tour.services.RiddleService;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@@ -32,9 +35,15 @@ public class RiddleController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(name = "Create a new riddle")
|
@PostMapping(name = "Create a new riddle")
|
||||||
public ResponseEntity<ReadRiddleData> createRiddle(@RequestBody CreateRiddleDTO createDto) {
|
public ResponseEntity<ReadRiddleData> createRiddle(@RequestBody CreateRiddle createDto) {
|
||||||
Riddle savedRiddle = riddleService.addRiddle(createDto);
|
Riddle savedRiddle = riddleService.addRiddle(createDto);
|
||||||
return ResponseEntity.status(HttpStatus.CREATED)
|
return ResponseEntity.status(HttpStatus.CREATED)
|
||||||
.body(riddleMapper.riddleToReadRiddleData(savedRiddle));
|
.body(riddleMapper.riddleToReadRiddleData(savedRiddle));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping(name = "Create Riddle Hint for Riddle ID", path = "/hints")
|
||||||
|
public ResponseEntity<RiddleHint> createRiddleHint(@RequestBody @Valid CreateRiddleHint createRiddleHint) {
|
||||||
|
RiddleHint savedHint = riddleService.addHintToRiddle(createRiddleHint);
|
||||||
|
return ResponseEntity.status(HttpStatus.CREATED).body(savedHint);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+1
-1
@@ -9,7 +9,7 @@ import lombok.NoArgsConstructor;
|
|||||||
@Builder
|
@Builder
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class CreateRiddleDTO {
|
public class CreateRiddle {
|
||||||
private String name;
|
private String name;
|
||||||
private Integer step;
|
private Integer step;
|
||||||
private Double latitude;
|
private Double latitude;
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package de.exodus_hn.excalibur_tour.dto.riddlehint;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class CreateRiddleHint {
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@JsonProperty(value = "riddleId", required = true)
|
||||||
|
private Long riddleId;
|
||||||
|
|
||||||
|
@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;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -16,7 +16,7 @@ public class RiddleHint {
|
|||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false, length = 4096)
|
||||||
private String hint;
|
private String hint;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
package de.exodus_hn.excalibur_tour.mapper;
|
package de.exodus_hn.excalibur_tour.mapper;
|
||||||
|
|
||||||
import de.exodus_hn.excalibur_tour.dto.riddle.CreateRiddleDTO;
|
import de.exodus_hn.excalibur_tour.dto.riddle.CreateRiddle;
|
||||||
import de.exodus_hn.excalibur_tour.dto.riddle.ReadRiddleData;
|
import de.exodus_hn.excalibur_tour.dto.riddle.ReadRiddleData;
|
||||||
|
import de.exodus_hn.excalibur_tour.dto.riddlehint.CreateRiddleHint;
|
||||||
import de.exodus_hn.excalibur_tour.dto.riddlehint.ReadRiddleHint;
|
import de.exodus_hn.excalibur_tour.dto.riddlehint.ReadRiddleHint;
|
||||||
import de.exodus_hn.excalibur_tour.entities.Riddle;
|
import de.exodus_hn.excalibur_tour.entities.Riddle;
|
||||||
import de.exodus_hn.excalibur_tour.entities.RiddleHint;
|
import de.exodus_hn.excalibur_tour.entities.RiddleHint;
|
||||||
@@ -24,7 +25,7 @@ public interface RiddleMapper {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
default Riddle toEntity(CreateRiddleDTO dto) {
|
default Riddle toEntity(CreateRiddle dto) {
|
||||||
if (dto == null) {
|
if (dto == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -51,4 +52,15 @@ public interface RiddleMapper {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default RiddleHint toEntity(CreateRiddleHint dto) {
|
||||||
|
if (dto == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return RiddleHint.builder()
|
||||||
|
.hintOrder(dto.getHintOrder())
|
||||||
|
.hint(dto.getHint())
|
||||||
|
.penaltyPoints(dto.getPenaltyPoints())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package de.exodus_hn.excalibur_tour.repository;
|
||||||
|
|
||||||
|
import de.exodus_hn.excalibur_tour.entities.Riddle;
|
||||||
|
import de.exodus_hn.excalibur_tour.entities.RiddleHint;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface RiddleHintRepository extends JpaRepository<RiddleHint, Long> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,22 +1,26 @@
|
|||||||
package de.exodus_hn.excalibur_tour.services;
|
package de.exodus_hn.excalibur_tour.services;
|
||||||
|
|
||||||
import de.exodus_hn.excalibur_tour.dto.riddle.CreateRiddleDTO;
|
import de.exodus_hn.excalibur_tour.dto.riddle.CreateRiddle;
|
||||||
import de.exodus_hn.excalibur_tour.dto.riddle.ReadRiddleData;
|
import de.exodus_hn.excalibur_tour.dto.riddlehint.CreateRiddleHint;
|
||||||
import de.exodus_hn.excalibur_tour.entities.Riddle;
|
import de.exodus_hn.excalibur_tour.entities.Riddle;
|
||||||
|
import de.exodus_hn.excalibur_tour.entities.RiddleHint;
|
||||||
import de.exodus_hn.excalibur_tour.mapper.RiddleMapper;
|
import de.exodus_hn.excalibur_tour.mapper.RiddleMapper;
|
||||||
|
import de.exodus_hn.excalibur_tour.repository.RiddleHintRepository;
|
||||||
import de.exodus_hn.excalibur_tour.repository.RiddleRepository;
|
import de.exodus_hn.excalibur_tour.repository.RiddleRepository;
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import jakarta.transaction.Transactional;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class RiddleService {
|
public class RiddleService {
|
||||||
|
|
||||||
private final RiddleRepository riddleRepository;
|
private final RiddleRepository riddleRepository;
|
||||||
|
private final RiddleHintRepository riddleHintRepository;
|
||||||
private final RiddleMapper riddleMapper;
|
private final RiddleMapper riddleMapper;
|
||||||
|
|
||||||
public @NotNull List<Riddle> getAllRiddles() {
|
public @NotNull List<Riddle> getAllRiddles() {
|
||||||
@@ -35,8 +39,21 @@ public class RiddleService {
|
|||||||
return riddleRepository.save(riddle);
|
return riddleRepository.save(riddle);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Riddle addRiddle(CreateRiddleDTO createDto) {
|
public Riddle addRiddle(CreateRiddle createDto) {
|
||||||
Riddle riddle = riddleMapper.toEntity(createDto);
|
Riddle riddle = riddleMapper.toEntity(createDto);
|
||||||
return riddleRepository.save(riddle);
|
return riddleRepository.save(riddle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public RiddleHint addHintToRiddle(CreateRiddleHint dto) {
|
||||||
|
Riddle riddle = riddleRepository.findById(dto.getRiddleId())
|
||||||
|
.orElseThrow(() -> new EntityNotFoundException("Riddle not found with ID: " + dto.getRiddleId()));
|
||||||
|
|
||||||
|
RiddleHint hint = riddleMapper.toEntity(dto);
|
||||||
|
|
||||||
|
hint.setRiddle(riddle);
|
||||||
|
riddle.getHints().add(hint);
|
||||||
|
|
||||||
|
return riddleHintRepository.save(hint);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user