Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
user edit
Browse files Browse the repository at this point in the history
  • Loading branch information
212851193 committed Dec 20, 2017
1 parent fcfa8ad commit accdab9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private ResponseEntity<Article> read(@PathVariable String id) {
return ResponseEntity.ok(read.iterator().next());
}

@Role(ADMIN)
@Role({ADMIN, EDITOR})
@PutMapping("/{id}")
private ResponseEntity<Article> edit(@PathVariable long id, @RequestBody Article article) {
Article updated = articleService.edit(id, article);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package hu.elte.alkfejl.hirportal.controller;

import hu.elte.alkfejl.hirportal.annotation.Role;
import hu.elte.alkfejl.hirportal.entity.Article;
import hu.elte.alkfejl.hirportal.entity.User;
import hu.elte.alkfejl.hirportal.service.UserService;
import hu.elte.alkfejl.hirportal.exception.UserNotValidException;
Expand All @@ -9,6 +11,8 @@
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import static hu.elte.alkfejl.hirportal.entity.User.Role.ADMIN;

@RestController
@RequestMapping("/api/user")
public class UserController {
Expand Down Expand Up @@ -46,10 +50,8 @@ public ResponseEntity<User> logout(@RequestBody User user) {
}

@GetMapping("/list")
public String userList(Model model) {
model.addAttribute("title", "User list");
model.addAttribute("users", userService.userNamesStartingWith(""));
return "userlist";
public ResponseEntity<Iterable<User>> list() {
return ResponseEntity.ok(userService.list());
}

@GetMapping("/greeting")
Expand All @@ -61,4 +63,11 @@ public String greeting(@RequestParam(value = "name", required = false, defaultVa
private String redirectToGreeting(@ModelAttribute User user) {
return "redirect:/user/greeting?name=" + user.getFirstname();
}

@Role(ADMIN)
@PutMapping("/{id}")
private ResponseEntity<User> edit(@PathVariable long id, @RequestBody User user) {
User updated = userService.edit(id, user);
return ResponseEntity.ok(updated);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hu.elte.alkfejl.hirportal.service;

import hu.elte.alkfejl.hirportal.entity.Article;
import hu.elte.alkfejl.hirportal.entity.User;
import hu.elte.alkfejl.hirportal.exception.UserNotValidException;
import hu.elte.alkfejl.hirportal.repository.UserRepository;
Expand All @@ -23,6 +24,14 @@ public List<User> userNamesStartingWith(String segment) {
return userRepository.findByFirstNameSegment(segment);
}

public User edit(long id, User user) {
return this.user = userRepository.save(user);
}

public Iterable<User> list() {
return userRepository.findAll();
}

public User register(User user) {
user.setRole(User.Role.READER);
return this.user = userRepository.save(user);
Expand Down

0 comments on commit accdab9

Please sign in to comment.