handle responses more properly
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package be.naaturel.letsmeet.controllers;
|
package be.naaturel.letsmeet.controllers;
|
||||||
|
|
||||||
|
import be.naaturel.letsmeet.core.Result;
|
||||||
import be.naaturel.letsmeet.dto.httpRequest.EventDTO;
|
import be.naaturel.letsmeet.dto.httpRequest.EventDTO;
|
||||||
import be.naaturel.letsmeet.services.EventService;
|
import be.naaturel.letsmeet.services.EventService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -26,7 +27,7 @@ public class EventController {
|
|||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
return ResponseEntity
|
return ResponseEntity
|
||||||
.internalServerError()
|
.internalServerError()
|
||||||
.build();
|
.body("An error has occured : " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,13 +35,12 @@ public class EventController {
|
|||||||
public ResponseEntity<?> create(@RequestBody EventDTO dto){
|
public ResponseEntity<?> create(@RequestBody EventDTO dto){
|
||||||
|
|
||||||
try {
|
try {
|
||||||
service.save(dto);
|
Result<String> res = service.save(dto);
|
||||||
|
return ResponseEntity.ok(res);
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
return ResponseEntity
|
return ResponseEntity
|
||||||
.internalServerError()
|
.internalServerError()
|
||||||
.build();
|
.body("An error has occured : " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
return ResponseEntity.ok().build();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
18
back/src/main/java/be/naaturel/letsmeet/core/Result.java
Normal file
18
back/src/main/java/be/naaturel/letsmeet/core/Result.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package be.naaturel.letsmeet.core;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
|
|
||||||
|
@JsonSerialize
|
||||||
|
public class Result<T> {
|
||||||
|
|
||||||
|
private T value;
|
||||||
|
|
||||||
|
public Result(T value){
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package be.naaturel.letsmeet.services;
|
package be.naaturel.letsmeet.services;
|
||||||
|
|
||||||
|
import be.naaturel.letsmeet.core.Result;
|
||||||
import be.naaturel.letsmeet.mappers.Mapper;
|
import be.naaturel.letsmeet.mappers.Mapper;
|
||||||
|
|
||||||
public abstract class AbstractService<T, T_ENTITY, T_DTO> {
|
public abstract class AbstractService<T, T_ENTITY, T_DTO> {
|
||||||
@@ -12,7 +13,7 @@ public abstract class AbstractService<T, T_ENTITY, T_DTO> {
|
|||||||
this.requestMapper = requestMapper;
|
this.requestMapper = requestMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract boolean save(T_DTO prop);
|
public abstract Result<?> save(T_DTO prop) throws Exception;
|
||||||
public abstract boolean delete(T_DTO prop);
|
public abstract boolean delete(T_DTO prop);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package be.naaturel.letsmeet.services;
|
package be.naaturel.letsmeet.services;
|
||||||
|
|
||||||
|
import be.naaturel.letsmeet.core.Result;
|
||||||
import be.naaturel.letsmeet.dto.database.EventEntity;
|
import be.naaturel.letsmeet.dto.database.EventEntity;
|
||||||
import be.naaturel.letsmeet.dto.httpRequest.EventDTO;
|
import be.naaturel.letsmeet.dto.httpRequest.EventDTO;
|
||||||
import be.naaturel.letsmeet.mappers.database.EventMapper;
|
import be.naaturel.letsmeet.mappers.database.EventMapper;
|
||||||
@@ -22,16 +23,14 @@ public class EventService extends AbstractService<Event, EventEntity, EventDTO>
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean save(EventDTO dto) {
|
public Result<String> save(EventDTO dto) throws Exception {
|
||||||
Event event = this.requestMapper.toModel(dto);
|
Event event = this.requestMapper.toModel(dto);
|
||||||
EventEntity entity = this.dataBaseMapper.toEntity(event);
|
EventEntity entity = this.dataBaseMapper.toEntity(event);
|
||||||
try{
|
try{
|
||||||
this.repo.save(entity);
|
this.repo.save(entity);
|
||||||
return true;
|
return new Result<>(entity.token);
|
||||||
} catch (IllegalArgumentException iae){
|
} catch (Exception e) {
|
||||||
return false;
|
throw new Exception("Something went wrong");
|
||||||
} catch (OptimisticLockingFailureException olfe){
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user