Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
dima767 committed Dec 12, 2018
1 parent edf035d commit 0165717
Showing 1 changed file with 14 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@
import edu.internet2.tier.shibboleth.admin.ui.controller.ErrorResponse;
import edu.internet2.tier.shibboleth.admin.ui.security.model.User;
import edu.internet2.tier.shibboleth.admin.ui.security.repository.UserRepository;
<<<<<<< Updated upstream

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
=======
import org.springframework.beans.factory.annotation.Required;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
>>>>>>> Stashed changes

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -53,27 +49,21 @@ public List<User> getAll() {
}

@Transactional(readOnly = true)
@GetMapping("/user/{username}")
@GetMapping("/users/{username}")
public ResponseEntity<?> getOne(@PathVariable String username) {
return ResponseEntity.ok(findUserOrThrowHttp404(username));
}

@Transactional
@DeleteMapping("/user/{username}")
@DeleteMapping("/users/{username}")
public ResponseEntity<?> deleteOne(@PathVariable String username) {
User user = findUserOrThrowHttp404(username);
userRepository.delete(user);
return ResponseEntity.noContent().build();
}


private User findUserOrThrowHttp404(String username) {
return userRepository.findByUsername(username)
.orElseThrow(() -> new HttpClientErrorException(NOT_FOUND, String.format("User with username [%s] not found", username)));
}

@Transactional
@PostMapping("/user")
@PostMapping("/users")
ResponseEntity<?> saveOne(@RequestParam User user) {
Optional<User> persistedUser = userRepository.findByUsername(user.getUsername());
if (persistedUser.isPresent()) {
Expand All @@ -88,7 +78,7 @@ ResponseEntity<?> saveOne(@RequestParam User user) {
}

@Transactional
@PutMapping("/user/{username}")
@PutMapping("/users/{username}")
ResponseEntity<?> updateOne(@PathVariable(value = "username") String username, @RequestParam User user) {
Optional<User> userSearchResult = userRepository.findByUsername(username);
if (!userSearchResult.isPresent()) {
Expand All @@ -98,11 +88,16 @@ ResponseEntity<?> updateOne(@PathVariable(value = "username") String username, @
}
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
User persistedUser = userSearchResult.get();
persistedUser.setPassword(passwordEncoder.encode(user.getPassword());
persistedUser.setPassword(passwordEncoder.encode(user.getPassword()));
persistedUser.setFirstName(user.getFirstName());
persistedUser.setLastName(user.getLastName());
persistedUser.setRoles(user.getRoles());
User savedUser = userRepository.save(persistedUser);
return ResponseEntity.ok(savedUser);
}

private User findUserOrThrowHttp404(String username) {
return userRepository.findByUsername(username)
.orElseThrow(() -> new HttpClientErrorException(NOT_FOUND, String.format("User with username [%s] not found", username)));
}
}

0 comments on commit 0165717

Please sign in to comment.