diff --git a/backend/src/main/java/meowhub/backend/posts/controllers/PostController.java b/backend/src/main/java/meowhub/backend/posts/controllers/PostController.java index 81fc607..d96e989 100644 --- a/backend/src/main/java/meowhub/backend/posts/controllers/PostController.java +++ b/backend/src/main/java/meowhub/backend/posts/controllers/PostController.java @@ -42,7 +42,7 @@ public ResponseEntity> getPostsForUser(@PathVariable("login") Stri } @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) - public ResponseEntity createPost(@RequestPart("content") String content, @RequestPart(value = "pictures", required = false) List pictures, @AuthenticationPrincipal UserDetails userDetails) { + public ResponseEntity createPost(@RequestPart(value = "content", required = false) String content, @RequestPart(value = "pictures", required = false) List pictures, @AuthenticationPrincipal UserDetails userDetails) { PostDto postDto = postService.createPost(userDetails.getUsername(), content, pictures); return ResponseEntity.ok(postDto); } diff --git a/backend/src/main/java/meowhub/backend/posts/services/impl/PostServiceImpl.java b/backend/src/main/java/meowhub/backend/posts/services/impl/PostServiceImpl.java index 474efd3..d1f5433 100644 --- a/backend/src/main/java/meowhub/backend/posts/services/impl/PostServiceImpl.java +++ b/backend/src/main/java/meowhub/backend/posts/services/impl/PostServiceImpl.java @@ -61,6 +61,10 @@ public Page getPostsForUser(String login, String requestedBy, int pageN @Override @Transactional public PostDto createPost(String login, String content, List pictures) { + if((content == null || content.isEmpty()) && (pictures == null || pictures.isEmpty())) { + throw new NullPointerException(AlertConstants.POST_CONTENT_OR_PICTURE_REQUIRED); + } + User postOwner = userPostServiceFacade.findUserByLogin(login); Post post = new Post(); post.setContentHtml(content); diff --git a/backend/src/main/java/meowhub/backend/shared/constants/AlertConstants.java b/backend/src/main/java/meowhub/backend/shared/constants/AlertConstants.java index 6071c15..46723ec 100644 --- a/backend/src/main/java/meowhub/backend/shared/constants/AlertConstants.java +++ b/backend/src/main/java/meowhub/backend/shared/constants/AlertConstants.java @@ -14,6 +14,8 @@ public class AlertConstants { public static final String NOT_UNIQUE_OBJECT_TITLE = "Not unique object"; public static final String ILLEGAL_ARGUMENT_TITLE = "Illegal argument"; + public static final String VALUE_REQUIRED_TITLE = "Value required"; + //message public static final String USER_WITH_LOGIN_NOT_FOUND = "User with login '%s' not found"; @@ -25,4 +27,6 @@ public class AlertConstants { public static final String BAD_CREDENTIALS = "Bad credentials"; public static final String NOT_UNIQUE_OBJECT = "%s:'%s' is not unique"; public static final String ILLEGAL_ARGUMENT = "%s cannot be equal %s"; + + public static final String POST_CONTENT_OR_PICTURE_REQUIRED = "Post content or picture is required"; } diff --git a/backend/src/main/java/meowhub/backend/shared/handlers/GlobalExceptionHandler.java b/backend/src/main/java/meowhub/backend/shared/handlers/GlobalExceptionHandler.java index 18bea03..b9e8edf 100644 --- a/backend/src/main/java/meowhub/backend/shared/handlers/GlobalExceptionHandler.java +++ b/backend/src/main/java/meowhub/backend/shared/handlers/GlobalExceptionHandler.java @@ -56,4 +56,8 @@ public ResponseEntity handleIllegalArgumentException(IllegalArgumentEx return new ResponseEntity<>(AlertUtils.illegalArgumentException(ex.getMessage()), HttpStatus.NOT_ACCEPTABLE); } + @ExceptionHandler(NullPointerException.class) + public ResponseEntity handleIllegalStateException(NullPointerException ex) { + return new ResponseEntity<>(AlertUtils.valueRequired(ex.getMessage()), HttpStatus.NOT_ACCEPTABLE); + } } diff --git a/backend/src/main/java/meowhub/backend/shared/utils/AlertUtils.java b/backend/src/main/java/meowhub/backend/shared/utils/AlertUtils.java index 41ebbf6..b922b20 100644 --- a/backend/src/main/java/meowhub/backend/shared/utils/AlertUtils.java +++ b/backend/src/main/java/meowhub/backend/shared/utils/AlertUtils.java @@ -89,4 +89,14 @@ public static AlertDto illegalArgumentException(String msg) { return alertDto; } + + public static AlertDto valueRequired(String msg){ + AlertDto alertDto = new AlertDto(); + alertDto.setTitle(AlertConstants.VALUE_REQUIRED_TITLE); + alertDto.setMessage(msg); + alertDto.setLevel(AlertLevel.ERROR); + alertDto.setTimestamp(LocalDateTime.now()); + + return alertDto; + } } diff --git a/backend/src/test/java/meowhub/backend/users/UserRepositoryIntegrationTest.java b/backend/src/test/java/meowhub/backend/users/UserRepositoryIntegrationTest.java index 850b712..faa9ae2 100644 --- a/backend/src/test/java/meowhub/backend/users/UserRepositoryIntegrationTest.java +++ b/backend/src/test/java/meowhub/backend/users/UserRepositoryIntegrationTest.java @@ -24,7 +24,7 @@ class UserRepositoryIntegrationTest { private UserRepository userRepository; @Test - void testFindUserByLogin(){ + void testFindUserByLogin() { Optional result = userRepository.findByLogin("admin"); assertTrue(result.isPresent()); assertEquals("admin", result.get().getLogin()); @@ -39,6 +39,5 @@ void testFindBasicUserInfoByLogin() { assertNotNull(result.get().getId()); assertNotNull(result.get().getName()); assertNotNull(result.get().getSurname()); -// assertNotNull(result.get().getProfilePicture()); } } \ No newline at end of file diff --git a/database/scripts/120_create_tables.sql b/database/scripts/120_create_tables.sql index d06de19..ff3b584 100644 --- a/database/scripts/120_create_tables.sql +++ b/database/scripts/120_create_tables.sql @@ -66,7 +66,7 @@ CREATE TABLE mh_posts.Posts ( id varchar2(36) DEFAULT sys_guid() NOT NULL, user_id varchar2(36) NOT NULL, - content_Html clob NOT NULL, + content_Html clob NULL, created_at date NOT NULL, created_by varchar2(36) NOT NULL, modified_at date NULL, diff --git a/frontend/src/Features/CreatePost/CreatePost.tsx b/frontend/src/Features/CreatePost/CreatePost.tsx index 801634f..a63456e 100644 --- a/frontend/src/Features/CreatePost/CreatePost.tsx +++ b/frontend/src/Features/CreatePost/CreatePost.tsx @@ -29,12 +29,7 @@ export const CreatePost = () => { return; } - api.post('/api/posts', null, { - params: {content: contentToSave}, - headers: { - 'Content-Type': 'multipart/form-data' - } - }).then((response) => { + api.post('/api/posts', null, {params: {content: contentToSave}}).then((response) => { if (response.status === 200) { close(); }