Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@
<version>2.6.0</version>
</dependency>

<!-- MapStruct -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.6.0</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
35 changes: 9 additions & 26 deletions src/main/java/com/hacktober/blog/user/User.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,21 @@
package com.hacktober.blog.user;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class User {

private String name;
private String username; // document id
private String email;
private String password; // will be stored as Base64 encrypted
private List<String> blogs;

public User() {}

public User(String name, String username, String email, String password, List<String> blogs) {
this.name = name;
this.username = username;
this.email = email;
this.password = password;
this.blogs = blogs;
}

// Getters & Setters
public String getName() { return name; }
public void setName(String name) { this.name = name; }

public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }

public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }

public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }

public List<String> getBlogs() { return blogs; }
public void setBlogs(List<String> blogs) { this.blogs = blogs; }
}
20 changes: 12 additions & 8 deletions src/main/java/com/hacktober/blog/user/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import java.util.concurrent.ExecutionException;

import com.hacktober.blog.exceptions.ResourceNotFoundException;
import com.hacktober.blog.user.dto.UserDto;
import com.hacktober.blog.user.dto.UserMapper;
import com.hacktober.blog.utils.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -14,19 +17,18 @@

@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
@CrossOrigin("*")
@Tag(name = "Users", description = "Endpoints for managing platform users")
public class UserController {

private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
private final UserMapper userMapper;

@PostMapping("/create")
@Operation(summary = "Create user", description = "Persist a new user profile in the database.")
public ResponseEntity<ApiResponse<String>> createUser(@RequestBody User user) throws InterruptedException, ExecutionException {
String result = userService.create(user);
public ResponseEntity<ApiResponse<String>> createUser(@RequestBody UserDto user) throws InterruptedException, ExecutionException {
String result = userService.create(userMapper.fromDtoToEntity(user));
return ResponseEntity.status(HttpStatus.CREATED)
.body(ApiResponse.success(result, "User created successfully"));
}
Expand All @@ -43,9 +45,11 @@ public ResponseEntity<ApiResponse<User>> getUser(@PathVariable String username)

@GetMapping("/all")
@Operation(summary = "List users", description = "Retrieve all registered users.")
public ResponseEntity<ApiResponse<List<User>>> getAllUsers() throws InterruptedException, ExecutionException {
List<User> users = userService.getAll();
return ResponseEntity.ok(ApiResponse.success(users, "Users retrieved successfully"));
public ResponseEntity<ApiResponse<List<UserDto>>> getAllUsers() throws InterruptedException, ExecutionException {
List<UserDto> users = userService.getAll().stream()
.map(userMapper::fromEntityToDto)
.toList();
return ResponseEntity.ok(ApiResponse.success(users, "Users retrieved successfully"));
}

@PutMapping("/{username}")
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/hacktober/blog/user/dto/UserDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.hacktober.blog.user.dto;

import java.util.List;

public record UserDto(String name, String username, String email, String password, List<String> blogs) {
}
10 changes: 10 additions & 0 deletions src/main/java/com/hacktober/blog/user/dto/UserMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.hacktober.blog.user.dto;

import com.hacktober.blog.user.User;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface UserMapper {
User fromDtoToEntity(UserDto userDto);
UserDto fromEntityToDto(User user);
}
13 changes: 0 additions & 13 deletions src/test/java/com/hacktober/blog/BlogApplicationTests.java

This file was deleted.