implement API Json error and generator
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is passing
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	continuous-integration/drone/push Build is passing
				
			This commit is contained in:
		
							
								
								
									
										33
									
								
								src/main/java/wtf/beatrice/releasehive/model/ApiError.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								src/main/java/wtf/beatrice/releasehive/model/ApiError.java
									
									
									
									
									
										Normal 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; | ||||
|     } | ||||
| } | ||||
| @@ -6,8 +6,8 @@ import wtf.beatrice.releasehive.util.JsonUtil; | ||||
| import wtf.beatrice.releasehive.model.User; | ||||
| import wtf.beatrice.releasehive.service.AccountService; | ||||
|  | ||||
| @RequestMapping("/api/v1/users") | ||||
| @RestController | ||||
| @RequestMapping("/api/v1/users") | ||||
| public class AccountResource { | ||||
|  | ||||
|     private final AccountService accountService; | ||||
| @@ -22,7 +22,6 @@ public class AccountResource { | ||||
|             produces="application/json") | ||||
|     public String register(@RequestBody User user) | ||||
|     { | ||||
|         accountService.registerUser(user); | ||||
|         return JsonUtil.convertToJson(user); | ||||
|         return accountService.registerUser(user); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -5,5 +5,5 @@ import wtf.beatrice.releasehive.model.User; | ||||
| public interface AccountService | ||||
| { | ||||
|  | ||||
|     void registerUser(User user); | ||||
|     String registerUser(User user); | ||||
| } | ||||
|   | ||||
| @@ -5,15 +5,28 @@ import org.hibernate.Transaction; | ||||
| import org.springframework.stereotype.Service; | ||||
| import wtf.beatrice.releasehive.db.HibernateManager; | ||||
| import wtf.beatrice.releasehive.model.User; | ||||
| import wtf.beatrice.releasehive.util.JsonUtil; | ||||
|  | ||||
| @Service | ||||
| public class AccountServiceImpl implements AccountService { | ||||
|  | ||||
|     @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(); | ||||
|         Transaction transaction = session.beginTransaction(); | ||||
|         session.save(user); | ||||
|         session.persist(user); | ||||
|         transaction.commit(); | ||||
|  | ||||
|         return JsonUtil.convertToJson(user); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; | ||||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||||
| import org.apache.logging.log4j.LogManager; | ||||
| import org.apache.logging.log4j.Logger; | ||||
| import wtf.beatrice.releasehive.model.ApiError; | ||||
|  | ||||
| public class JsonUtil | ||||
| { | ||||
| @@ -23,4 +24,12 @@ public class JsonUtil | ||||
|             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; | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user