Add logic for event retrieval
This commit is contained in:
@@ -4,10 +4,7 @@ import be.naaturel.letsmeet.dto.httpRequest.EventDTO;
|
||||
import be.naaturel.letsmeet.services.EventService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
public class EventController {
|
||||
@@ -19,8 +16,22 @@ public class EventController {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
|
||||
@GetMapping({"/event", "/event/{token}"})
|
||||
public ResponseEntity<?> get(@PathVariable String token){
|
||||
|
||||
try{
|
||||
EventDTO dto = service.getEvent(token);
|
||||
return ResponseEntity.ok(dto);
|
||||
} catch (Exception e){
|
||||
return ResponseEntity
|
||||
.internalServerError()
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping({"/create", "/create/"})
|
||||
public ResponseEntity<?> submit(@RequestBody EventDTO dto){
|
||||
public ResponseEntity<?> create(@RequestBody EventDTO dto){
|
||||
|
||||
try{
|
||||
service.save(dto);
|
||||
@@ -32,10 +43,4 @@ public class EventController {
|
||||
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@GetMapping({"/join", "/join/"})
|
||||
public ResponseEntity<?> leaderboard(){
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,15 +23,11 @@ public class ParticipantMapper implements Mapper<Participant, ParticipantEntity>
|
||||
@Override
|
||||
public ParticipantEntity toEntity(Participant d) {
|
||||
return DatabasePropsFactory.createParticipant(d.getName(), dateMapper.toEntities(d.getDates(), HashSet::new));
|
||||
/*ParticipantEntity pe = new ParticipantEntity();
|
||||
pe.name = d.getName();
|
||||
pe.dates = dateMapper.toEntities(d.getDates(), HashSet::new);
|
||||
return pe;*/
|
||||
}
|
||||
|
||||
@Override
|
||||
public Participant toModel(ParticipantEntity d) {
|
||||
return new Participant(d.name);
|
||||
return new Participant(d.name, dateMapper.toModels(d.dates, HashSet::new));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -19,7 +19,7 @@ public class EventDTOMapper implements Mapper<Event, EventDTO> {
|
||||
EventDTO eventDTO = new EventDTO();
|
||||
eventDTO.name = event.getName();
|
||||
eventDTO.participants = participantMapper.toEntities(event.getParticipants(), HashSet::new);
|
||||
return new EventDTO();
|
||||
return eventDTO;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -3,12 +3,13 @@ package be.naaturel.letsmeet.repositories;
|
||||
import be.naaturel.letsmeet.dto.database.EventEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface EventRepo
|
||||
extends JpaRepository<EventEntity, String> {
|
||||
|
||||
@Query(value = "SELECT * FROM events;", nativeQuery = true)
|
||||
List<EventEntity> findEventEntityById(String id);
|
||||
@Query(value = "SELECT * FROM events e WHERE e.token = :#{#token};", nativeQuery = true)
|
||||
EventEntity findEventEntityByToken(@Param("token") String token);
|
||||
}
|
||||
|
||||
@@ -1,17 +1,13 @@
|
||||
package be.naaturel.letsmeet.services;
|
||||
|
||||
|
||||
import be.naaturel.letsmeet.mappers.Mapper;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public abstract class AbstractService<T, T_ENTITY, T_DTO> {
|
||||
|
||||
protected JpaRepository<T_ENTITY, String> repo;
|
||||
protected Mapper<T, T_ENTITY> dataBaseMapper;
|
||||
protected Mapper<T, T_DTO> requestMapper;
|
||||
|
||||
AbstractService(JpaRepository<T_ENTITY, String> repo, Mapper<T, T_ENTITY> dataBaseMapper, Mapper<T, T_DTO> requestMapper){
|
||||
this.repo = repo;
|
||||
AbstractService(Mapper<T, T_ENTITY> dataBaseMapper, Mapper<T, T_DTO> requestMapper){
|
||||
this.dataBaseMapper = dataBaseMapper;
|
||||
this.requestMapper = requestMapper;
|
||||
}
|
||||
|
||||
@@ -13,9 +13,12 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
public class EventService extends AbstractService<Event, EventEntity, EventDTO> {
|
||||
|
||||
protected EventRepo repo;
|
||||
|
||||
@Autowired
|
||||
public EventService(EventRepo eventRepo){
|
||||
super(eventRepo, new EventMapper(), new EventDTOMapper());
|
||||
super(new EventMapper(), new EventDTOMapper());
|
||||
this.repo = eventRepo;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -46,4 +49,14 @@ public class EventService extends AbstractService<Event, EventEntity, EventDTO>
|
||||
}
|
||||
}
|
||||
|
||||
public EventDTO getEvent(String token){
|
||||
try{
|
||||
EventEntity entity = this.repo.findEventEntityByToken(token);
|
||||
Event model = dataBaseMapper.toModel(entity);
|
||||
EventDTO dto = requestMapper.toEntity(model);
|
||||
return dto;
|
||||
} catch (Exception e){
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ sec.cors.authorizedMethods=GET,POST,PUT,DELETE,OPTION
|
||||
sec.cors.authorizedHeader=Authorization,Content-type
|
||||
|
||||
#=============DATABASE=============
|
||||
spring.datasource.url=jdbc:${DB_URL}
|
||||
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/letsmeet_db
|
||||
spring.datasource.username=${DB_USER}
|
||||
spring.datasource.password=${DB_PASSWORD}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user