implement API Json error and generator
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bea 2024-08-07 10:10:10 +02:00
parent fe03484427
commit f142912471
5 changed files with 60 additions and 6 deletions

View File

@ -0,0 +1,33 @@
package wtf.beatrice.releasehive.model;
import jakarta.persistence.Entity;
import java.util.UUID;
@Entity
public class ApiError
{
UUID exceptionId;
String message;
public ApiError() {
exceptionId = UUID.randomUUID();
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public UUID getExceptionId() {
return exceptionId;
}
public void setExceptionId(UUID exceptionId) {
this.exceptionId = exceptionId;
}
}

View File

@ -6,8 +6,8 @@ import wtf.beatrice.releasehive.util.JsonUtil;
import wtf.beatrice.releasehive.model.User; import wtf.beatrice.releasehive.model.User;
import wtf.beatrice.releasehive.service.AccountService; import wtf.beatrice.releasehive.service.AccountService;
@RequestMapping("/api/v1/users")
@RestController @RestController
@RequestMapping("/api/v1/users")
public class AccountResource { public class AccountResource {
private final AccountService accountService; private final AccountService accountService;
@ -22,7 +22,6 @@ public class AccountResource {
produces="application/json") produces="application/json")
public String register(@RequestBody User user) public String register(@RequestBody User user)
{ {
accountService.registerUser(user); return accountService.registerUser(user);
return JsonUtil.convertToJson(user);
} }
} }

View File

@ -5,5 +5,5 @@ import wtf.beatrice.releasehive.model.User;
public interface AccountService public interface AccountService
{ {
void registerUser(User user); String registerUser(User user);
} }

View File

@ -5,15 +5,28 @@ import org.hibernate.Transaction;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import wtf.beatrice.releasehive.db.HibernateManager; import wtf.beatrice.releasehive.db.HibernateManager;
import wtf.beatrice.releasehive.model.User; import wtf.beatrice.releasehive.model.User;
import wtf.beatrice.releasehive.util.JsonUtil;
@Service @Service
public class AccountServiceImpl implements AccountService { public class AccountServiceImpl implements AccountService {
@Override @Override
public void registerUser(User user) { public String registerUser(User user) {
if(null == user.getUsername() || user.getUsername().isEmpty()) {
return JsonUtil.spawnJsonError("Cannot register user without username");
}
if(null == user.getPassword() || user.getPassword().isEmpty()) {
return JsonUtil.spawnJsonError("Cannot register user without password");
}
Session session = HibernateManager.getSession(); Session session = HibernateManager.getSession();
Transaction transaction = session.beginTransaction(); Transaction transaction = session.beginTransaction();
session.save(user); session.persist(user);
transaction.commit(); transaction.commit();
return JsonUtil.convertToJson(user);
} }
} }

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import wtf.beatrice.releasehive.model.ApiError;
public class JsonUtil public class JsonUtil
{ {
@ -23,4 +24,12 @@ public class JsonUtil
return e.getMessage(); return e.getMessage();
} }
} }
public static String spawnJsonError(String errorMessage) {
ApiError apiError = new ApiError();
apiError.setMessage(errorMessage);
String error = convertToJson(apiError);
LOGGER.error(error);
return error;
}
} }