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
86 changes: 45 additions & 41 deletions src/main/java/com/staxrt/tutorial/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@

package com.staxrt.tutorial.controller;

import com.staxrt.tutorial.dto.PageResponseDto;
import com.staxrt.tutorial.dto.UserDto;
import com.staxrt.tutorial.dto.UserResponseDto;
import com.staxrt.tutorial.exception.ResourceNotFoundException;
import com.staxrt.tutorial.exception.UserAlreadyExistException;
import com.staxrt.tutorial.model.User;
import com.staxrt.tutorial.repository.UserRepository;
import com.staxrt.tutorial.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -32,6 +39,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
* The type User controller.
Expand All @@ -45,87 +54,82 @@ public class UserController {
@Autowired
private UserRepository userRepository;

@Autowired
UserService userService;

/**
* Get all users list.
*
* @return the list
*/
@GetMapping("/demo")
public String printHello() {
return "Hello World!";
}

@GetMapping("/users")
public List<User> getAllUsers() {
return userRepository.findAll();
public ResponseEntity<List<User>> getAllUsers() {
return ResponseEntity.ok(userService.findAllUserse());
}

/**
* Gets users by id.
*
* @param userId the user id
* @Pathvaraible userId the user id
* @return the users by id
* @throws ResourceNotFoundException the resource not found exception
*/
@GetMapping("/users/{id}")
public ResponseEntity<User> getUsersById(@PathVariable(value = "id") Long userId)
throws ResourceNotFoundException {
User user =
userRepository
.findById(userId)
.orElseThrow(() -> new ResourceNotFoundException("User not found on :: " + userId));
return ResponseEntity.ok().body(user);
public ResponseEntity<User> getUsersById(@PathVariable(value = "id") Long userId) {
return ResponseEntity.ok(userService.findByUserId(userId));
}

/**
* Create user user.
*
* @param user the user
* @RequestBody user
* @return the user
*/
@PostMapping("/users")
public User createUser(@Valid @RequestBody User user) {
return userRepository.save(user);
public ResponseEntity<UserResponseDto> createUser(@Valid @RequestBody UserDto user) {
return ResponseEntity.ok(userService.createUser(user));
}

/**
* Update user response entity.
*
* @param userId the user id
* @param userDetails the user details
* @RequestBody user the user id
*
* @return the response entity
* @throws ResourceNotFoundException the resource not found exception
*/
@PutMapping("/users/{id}")
public ResponseEntity<User> updateUser(
@PathVariable(value = "id") Long userId, @Valid @RequestBody User userDetails)
throws ResourceNotFoundException {

User user =
userRepository
.findById(userId)
.orElseThrow(() -> new ResourceNotFoundException("User not found on :: " + userId));

user.setEmail(userDetails.getEmail());
user.setLastName(userDetails.getLastName());
user.setFirstName(userDetails.getFirstName());
user.setUpdatedAt(new Date());
final User updatedUser = userRepository.save(user);
return ResponseEntity.ok(updatedUser);
public ResponseEntity<UserResponseDto> updateUser(
@PathVariable(value = "id") Long userId, @Valid @RequestBody UserDto userDetails) {

return ResponseEntity.ok(userService.updateuser(userId, userDetails));
}

/**
* Delete user map.
*
* @param userId the user id
* @Pathvaraible userId the user id
* @return the map
* @throws Exception the exception
*/
@DeleteMapping("/user/{id}")
public Map<String, Boolean> deleteUser(@PathVariable(value = "id") Long userId) throws Exception {
User user =
userRepository
.findById(userId)
.orElseThrow(() -> new ResourceNotFoundException("User not found on :: " + userId));

userRepository.delete(user);
Map<String, Boolean> response = new HashMap<>();
response.put("deleted", Boolean.TRUE);
return response;
public ResponseEntity<Map<String, Boolean>> deleteUser(@PathVariable(value = "id") Long userId) {
return ResponseEntity.ok(userService.deleteUser(userId));
}

@GetMapping("/users/getPaginatedusers")
public ResponseEntity<PageResponseDto<UserResponseDto>> getPaginatedUsers(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "5") int size,
@RequestParam(defaultValue = "id") String sortBy,
@RequestParam(defaultValue = "asc") String direction) {
PageResponseDto<UserResponseDto> users = userService.getUsers(page, size, sortBy, direction);
return ResponseEntity.ok(users);

}
}
5 changes: 5 additions & 0 deletions src/main/java/com/staxrt/tutorial/dto/CreateGroup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.staxrt.tutorial.dto;

public interface CreateGroup {

}
70 changes: 70 additions & 0 deletions src/main/java/com/staxrt/tutorial/dto/PageResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.staxrt.tutorial.dto;

import java.util.List;

public class PageResponseDto<T> {

private List<T> data;
private int page;
private int size;
private long totalElements;
private int totalPages;
private boolean first;
private boolean last;
public PageResponseDto(List<T> data, int page, int size, long totalElements, int totalPages, boolean first,
boolean last) {
this.data = data;
this.page = page;
this.size = size;
this.totalElements = totalElements;
this.totalPages = totalPages;
this.first = first;
this.last = last;
}
public List<T> getData() {
return data;
}
public void setData(List<T> data) {
this.data = data;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public long getTotalElements() {
return totalElements;
}
public void setTotalElements(long totalElements) {
this.totalElements = totalElements;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public boolean isFirst() {
return first;
}
public void setFirst(boolean first) {
this.first = first;
}
public boolean isLast() {
return last;
}
public void setLast(boolean last) {
this.last = last;
}




}
5 changes: 5 additions & 0 deletions src/main/java/com/staxrt/tutorial/dto/UpdateGroup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.staxrt.tutorial.dto;

public interface UpdateGroup {

}
74 changes: 74 additions & 0 deletions src/main/java/com/staxrt/tutorial/dto/UserDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.staxrt.tutorial.dto;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class UserDto {

@NotNull(message = "First name cannot be null")
@Size(min = 2, message = "First name should have at least 2 characters")
@Size(max = 50, message = "First name should have at most 50 characters")
private String firstName;

@NotNull(message = "Last name cannot be null")
@Size(min = 2, message = "Last name should have at least 2 characters")
@Size(max = 50, message = "Last name should have at most 50 characters")
private String lastName;

@NotNull(message = "Email cannot be null")
@Email(message = "Email should be valid")
@Size(max = 100, message = "Email should have at most 100 characters")
private String email;

public UserDto() {
}

public UserDto(@NotNull(message = "First name cannot be null") String firstName,
@NotNull(message = "Last name cannot be null") String lastName,
@NotNull(message = "Email cannot be null") @Email(message = "Email should be valid") String email
) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;

}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmail() {
return email;
}

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

@Override
public String toString() {
return "UserDto [firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
}









}
51 changes: 51 additions & 0 deletions src/main/java/com/staxrt/tutorial/dto/UserResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.staxrt.tutorial.dto;

import java.util.Date;

public class UserResponseDto {
private Long id;
private String firstname;
private String lastName;
private String email;




public UserResponseDto() {
}

public UserResponseDto(Long id, String firstname, String lastName, String email) {
this.id = id;
this.firstname = firstname;
this.lastName = lastName;
this.email = email;

}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}



}
Loading