Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User service update implementation #45

Merged
merged 11 commits into from
Oct 28, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.Objects;

/**
* Request object for user image creation.
* Request object for user image insertion.
*
* @author mstar
* @version 1.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.turkninja.petshop.api.request.user;

import lombok.Data;

import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

/**
* Request object holding updatable fields to update a user entity.
*
* @author mstar
* @version 1.0
* @since 1.0, 2021-10-14
*/
@Data
public class UserUpdateRequest {

@Size(min = 2, max = 20,
message = "İsim en az 2 en çok 20 karakter olmalıdır")
private String name;

@Size(min = 2, max = 25,
message = "Soyad en az 2 en çok 25 karakter olmalıdır")
private String lastName;

@Size(min = 10, max = 10,
message = "Telefon numarası 10 hane olmalıdır")
private String mobilePhone;

@Pattern(regexp = ("(?i)e|k"),
message = "ERKEK veya KADIN değerlerinden biri girilmelidir")
private String gender;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.turkninja.petshop.value;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.Column;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Phone {
@Column(name = "mobile_phone")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.turkninja.petshop.user;

import com.turkninja.petshop.api.request.user.UserImageInsertRequest;
import com.turkninja.petshop.api.request.user.UserSignupRequest;
import com.turkninja.petshop.api.request.user.UserUpdateRequest;
import com.turkninja.petshop.api.response.common.PageResponse;
import com.turkninja.petshop.api.response.user.UserImageInsertResponse;
import com.turkninja.petshop.api.response.user.UserResponse;
import org.springframework.security.core.userdetails.UserDetailsService;

Expand All @@ -12,6 +11,6 @@ public interface UserService extends UserDetailsService {
void signUp(UserSignupRequest request);
PageResponse<UserResponse> list(int page, int size);
UserResponse getOne(Long id);
UserResponse update(Long id, UserUpdateRequest request);
void delete(Long id);
UserImageInsertResponse insertUserImage(UserImageInsertRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

import com.turkninja.petshop.UserRepository;
import com.turkninja.petshop.UserRoleRepository;
import com.turkninja.petshop.api.request.user.UserImageInsertRequest;
import com.turkninja.petshop.api.request.user.UserSignupRequest;
import com.turkninja.petshop.api.request.user.UserUpdateRequest;
import com.turkninja.petshop.api.response.common.PageResponse;
import com.turkninja.petshop.api.response.user.UserImageInsertResponse;
import com.turkninja.petshop.api.response.user.UserResponse;
import com.turkninja.petshop.entity.user.UserEntity;
import com.turkninja.petshop.entity.user.UserRoleEntity;
import com.turkninja.petshop.enums.Gender;
import com.turkninja.petshop.exception.AppMessage;
import com.turkninja.petshop.exception.AppParameter;
import com.turkninja.petshop.exception.ApplicationException;
import com.turkninja.petshop.mapper.UserMapper;
import com.turkninja.petshop.user.UserService;
import com.turkninja.petshop.value.Phone;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
Expand All @@ -25,8 +26,10 @@
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

@Service
Expand Down Expand Up @@ -64,15 +67,16 @@ public void signUp(UserSignupRequest request) {

/**
* Finds the user with given id and returns a UserResponse object.
*
* @param id Id of the user.
* @return UserResponse representation of the found user.
* @throws ApplicationException
* @throws ApplicationException Returns RECORD_NOT_FOUND exception.
*/
@Override
public UserResponse getOne(Long id) throws ApplicationException {
Optional<UserEntity> optionalUserEntity = userRepository
.findByIdAndActiveTrue(id);
if(!optionalUserEntity.isPresent()) {
if (!optionalUserEntity.isPresent()) {
throw new ApplicationException(AppMessage.RECORD_NOT_FOUND);
}

Expand All @@ -81,10 +85,11 @@ public UserResponse getOne(Long id) throws ApplicationException {

/**
* Returns a paged response of users.
*
* @param page Page number.
* @param size Size of page.
* @return PageResponse representation of UserResponse.
* @throws ApplicationException
* @throws ApplicationException Returns ApplicationException.
*/
@Override
public PageResponse<UserResponse> list(int page, int size)
Expand All @@ -96,27 +101,70 @@ public PageResponse<UserResponse> list(int page, int size)

/**
* Deactivates a user.
*
* @param id Id of the user.
* @throws ApplicationException Returns RECORD_NOT_FOUND exception.
*/
@Override
public void delete(Long id)
throws ApplicationException {
UserEntity entity = userRepository.findByIdAndActiveTrue(id).orElseThrow(() ->
new ApplicationException(AppMessage.RECORD_NOT_FOUND,
AppParameter.get("userId", id)));
throws ApplicationException {
UserEntity entity = userRepository
.findByIdAndActiveTrue(id)
.orElseThrow(() ->
new ApplicationException(AppMessage.RECORD_NOT_FOUND,
AppParameter.get("userId", id)));
entity.setActive(false);
userRepository.save(entity);
}

/**
* Updates User entity with provided field values.
*
* @param id User id.
* @param request An object holding updatable fields and values.
* @throws ApplicationException Return RECORD_NOT_FOUND exception.
*/
@Override
public UserImageInsertResponse insertUserImage(UserImageInsertRequest request) {
return new UserImageInsertResponse();
public UserResponse update(Long id, @Valid UserUpdateRequest request)
throws ApplicationException {
UserEntity entity = userRepository
.findByIdAndActiveTrue(id)
.orElseThrow(() ->
new ApplicationException(AppMessage.RECORD_NOT_FOUND,
AppParameter.get("userId", id)));

String name = request.getName();
String lastName = request.getLastName();
String genderValue = request.getGender();
String mobilePhone = request.getMobilePhone();

if (name != null && !name.isBlank()) {
entity.getFullName().setName(name);
}

if (lastName != null && !lastName.isBlank()) {
entity.getFullName().setLastName(lastName);
}

if (genderValue != null && !genderValue.isBlank()) {
Gender gender = genderValue.toLowerCase(Locale.ROOT).equals("e")
? Gender.MALE : Gender.FEMALE;
entity.setGender(gender);
}

if (mobilePhone != null && !mobilePhone.isBlank()) {
Phone phone = new Phone(mobilePhone);
entity.setPhone(phone);
}

userRepository.save(entity);
return userMapper.entityToUserResponse(entity);
}

private void setDefaultUserRole(UserEntity userEntity){
private void setDefaultUserRole(UserEntity userEntity) {
Optional<UserRoleEntity> defaultUserRoleOptional = userRoleRepository.findById(1L);
UserRoleEntity userRoleEntity = null;
if (defaultUserRoleOptional.isPresent()){
if (defaultUserRoleOptional.isPresent()) {
userRoleEntity = defaultUserRoleOptional.get();
userRoleEntity.getUsers().add(userEntity);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.turkninja.petshop.v1;

import com.turkninja.petshop.api.request.user.UserSignupRequest;
import com.turkninja.petshop.api.request.user.UserUpdateRequest;
import com.turkninja.petshop.api.response.common.PageResponse;
import com.turkninja.petshop.api.response.user.UserResponse;
import com.turkninja.petshop.user.UserService;
Expand All @@ -9,7 +10,15 @@
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

Expand Down Expand Up @@ -47,14 +56,22 @@ public ResponseEntity<UserResponse> getUser(@PathVariable("id") Long id) {
return new ResponseEntity<>(userResponse, HttpStatus.OK);
}

@DeleteMapping("{id}")
@PatchMapping("/{id}")
public ResponseEntity<UserResponse> update(
@PathVariable("id") Long id,
@Valid @RequestBody UserUpdateRequest request) {
UserResponse userResponse = userService.update(id, request);
return new ResponseEntity<>(userResponse, HttpStatus.OK);
}

@DeleteMapping("/{id}")
public ResponseEntity<?> delete(@PathVariable("id") Long id) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth == null
|| auth
.getAuthorities()
.stream()
.anyMatch(r -> !r.getAuthority().equals("ADMIN"))) {
.anyMatch(r -> !r.getAuthority().equals("ROLE_ADMIN"))) {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
userService.delete(id);
ilkaypolat1985 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down