Schema.sql
package com.app.blog.controller;
import com.app.blog.dto.LoginDto;
import com.app.blog.dto.RegisterDto;
import com.app.blog.entity.User;
import com.app.blog.service.UserService;
import com.app.blog.util.EntityHawk;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
@RestController
public class AuthController {
@Autowired
private UserService userService;
@PostMapping("/register")
public ResponseEntity<?> register(@Valid @ModelAttribute RegisterDto registerDto, BindingResult result) {
if (result.hasErrors()) {
StringBuilder errors = new StringBuilder();
result.getFieldErrors().forEach(error ->
errors.append(error.getField()).append(" ").append(error.getDefaultMessage()).append("; ")
);
return ResponseEntity.badRequest().body(new EntityHawk().genericFailure(errors.toString().trim()));
}
User saved = userService.saveUser(registerDto);
return ResponseEntity.ok(new EntityHawk().genericSuccess(saved));
}
@PostMapping("/login")
public ResponseEntity<?> login(@ModelAttribute LoginDto loginDto) {
String token = userService.login(loginDto);
if (token == null) {
return ResponseEntity.badRequest().body(new EntityHawk().genericFailure("Invalid credentials"));
}
return ResponseEntity.ok(new EntityHawk().genericSuccess(token));
}
}
package com.app.blog.controller;
import com.app.blog.dto.PostDto;
import com.app.blog.service.PostService;
import com.app.blog.util.EntityHawk;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class PostController {
@Autowired
private PostService postService;
@PostMapping("/publish")
public ResponseEntity<?> publish(@ModelAttribute PostDto postDto, Authentication auth) {
return ResponseEntity.ok(new EntityHawk().genericSuccess(
postService.publish(postDto, auth.getName())
));
}
@GetMapping("/getPost")
public ResponseEntity<?> getAllPosts() {
return ResponseEntity.ok(new EntityHawk().genericSuccess(postService.getAll()));
}
@GetMapping("/getPostCount")
public ResponseEntity<?> getPostCount() {
return ResponseEntity.ok(new EntityHawk().genericSuccess(postService.getCount()));
}
@GetMapping("/getPostByUser/{userId}")
public ResponseEntity<?> getPostByUser(@PathVariable Integer userId) {
return ResponseEntity.ok(new EntityHawk().genericSuccess(postService.getByUser(userId)));
}
@GetMapping("/getPost/{postId}")
public ResponseEntity<?> getPost(@PathVariable Integer postId) {
return ResponseEntity.ok(new EntityHawk().genericSuccess(postService.getById(postId)));
}
@PostMapping("/updatePost")
public ResponseEntity<?> update(@ModelAttribute PostDto postDto, Authentication auth) {
boolean result = postService.update(postDto, auth.getName());
if (!result) {
return ResponseEntity.badRequest()
.body(new EntityHawk().genericFailure("You are not allowed to update this post"));
}
return ResponseEntity.ok(new EntityHawk().genericSuccess("Post updated successfully"));
}
@GetMapping("/deletePost/{postId}")
public ResponseEntity<?> delete(@PathVariable Integer postId, Authentication auth) {
boolean result = postService.delete(postId, auth.getName());
if (!result) {
return ResponseEntity.badRequest()
.body(new EntityHawk().genericFailure("You are not allowed to delete this post"));
}
return ResponseEntity.ok(new EntityHawk().genericSuccess("Post deleted successfully"));
}
}
package com.app.blog.util;
import java.util.HashMap;
import java.util.Map;
public class EntityHawk {
public Map<String, Object> genericSuccess(Object data) {
Map<String, Object> response = new HashMap<>();
response.put("success", true);
response.put("data", data);
return response;
}
public Map<String, Object> genericFailure(String message) {
Map<String, Object> response = new HashMap<>();
response.put("success", false);
response.put("data", message); // Note: message is placed in the 'data' field as per test expectation
return response;
}
}