Advertisement
Guest User

Untitled

a guest
Feb 7th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.18 KB | None | 0 0
  1. @MappedSuperclass
  2. public abstract class BaseEntity {
  3.  
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.IDENTITY)
  6. @JsonIgnore
  7. private Long id;
  8. }
  9.  
  10. @Entity
  11. @Table(name = "TO_DO_ITEMS")
  12. public class ToDoItem extends BaseEntity {
  13.  
  14. @Column(name = "TITLE", nullable = false)
  15. private String title;
  16.  
  17. @Column(name = "COMPLETED")
  18. private boolean completed;
  19.  
  20. @Column(name = "DUE_DATE", nullable = false)
  21. @Convert(converter = LocalDateAttributeConverter.class)
  22. @JsonDeserialize(using = LocalDateDeserializer.class)
  23. @JsonSerialize(using = LocalDateSerializer.class)
  24. private LocalDate dueDate;
  25.  
  26. // a ToDoItem is only associated with one user
  27. @ManyToOne(cascade = CascadeType.PERSIST)
  28. @JsonIgnore
  29. @JoinColumn(name = "USER_ID")
  30. private User user;
  31.  
  32. // JPA demands empty constructor
  33. public ToDoItem() {
  34. }
  35.  
  36. public ToDoItem(User user, String title, LocalDate dueDate) {
  37. this.user = user;
  38. this.title = title;
  39. this.dueDate = dueDate;
  40. }
  41.  
  42. @Entity
  43. @Table(name = "USERS")
  44. public class User extends BaseEntity {
  45.  
  46. @Column(name = "USERNAME")
  47. private String username;
  48.  
  49. @Column(name = "PASSWORD")
  50. private String password;
  51.  
  52. @Column(name = "EMAIL")
  53. private String email;
  54.  
  55. // user can have many ToDoItems
  56. @JsonIgnore
  57. @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
  58. private Set<ToDoItem> toDoItems = new HashSet<>();
  59.  
  60. // JPA demands empty constructor
  61. public User() {
  62. }
  63.  
  64. public User(String username, String password, String email) {
  65. this.username = username;
  66. this.password = password;
  67. this.email = email;
  68. }
  69.  
  70. public interface UserRepository extends CrudRepository<User, Long> {
  71. User findByUsername(String username);
  72. }
  73.  
  74. public interface ToDoItemRepository extends CrudRepository<ToDoItem, Long> {}
  75.  
  76. @Service
  77. @Transactional
  78. public class ToDoItemServiceBean {
  79. private final ToDoItemRepository toDoItemRepository;
  80.  
  81. @Autowired
  82. public ToDoItemServiceBean(ToDoItemRepository toDoItemRepository) {
  83. this.toDoItemRepository = toDoItemRepository;
  84. }
  85.  
  86. public ToDoItem addToDo(ToDoItem toDoItem, User user) {
  87. String toDoTitle = toDoItem.getTitle();
  88. LocalDate toDoDueDate = toDoItem.getDueDate();
  89. ToDoItem newToDo = new ToDoItem(user, toDoTitle, toDoDueDate);
  90. return toDoItemRepository.save(newToDo);
  91. }
  92.  
  93. public ToDoItem editToDo(ToDoItem newToDoItem, ToDoItem oldToDoItem) {
  94. String newTitle = newToDoItem.getTitle();
  95. LocalDate newDueDate = newToDoItem.getDueDate();
  96. oldToDoItem.setTitle(newTitle);
  97. oldToDoItem.setDueDate(newDueDate);
  98. return oldToDoItem;
  99. }
  100.  
  101. public void deleteToDo(Long id) {
  102. toDoItemRepository.delete(id);
  103. }
  104.  
  105. public void completeToDo(ToDoItem toDoItem) {
  106. toDoItem.setCompleted(true);
  107. }
  108.  
  109. public boolean toDoExists(Long id) {
  110. if (toDoItemRepository.findOne(id) != null) {
  111. return true;
  112. }
  113. return false;
  114. }
  115.  
  116. public boolean canUserAccessToDo(ToDoItem toDoItem, User user) {
  117. if (toDoItem.getUser() == user) {
  118. return true;
  119. }
  120. return false;
  121. }
  122.  
  123. public ToDoItem findToDoItemById(Long id) {
  124. return toDoItemRepository.findOne(id);
  125. }
  126. }
  127.  
  128. @Service
  129. @Transactional
  130. public class UserServiceBean {
  131.  
  132. private final UserRepository userRepository;
  133. private final PasswordEncoder passwordEncoder;
  134.  
  135. @Autowired
  136. public UserServiceBean(UserRepository userRepository, PasswordEncoder passwordEncoder) {
  137. this.userRepository = userRepository;
  138. this.passwordEncoder = passwordEncoder;
  139. }
  140.  
  141. public User saveUser(User user) {
  142. User newUser = new User();
  143. newUser.setUsername(user.getUsername());
  144. newUser.setEmail(user.getEmail());
  145. newUser.setPassword(passwordEncoder.encode(user.getPassword()));
  146. return userRepository.save(newUser);
  147. }
  148.  
  149. public boolean userExists(User user) {
  150. if (userRepository.findByUsername(user.getUsername()) == null) {
  151. return false;
  152. }
  153. return true;
  154. }
  155.  
  156. public Iterable<ToDoItem> getAllToDoItems(User user) {
  157. return user.getToDoItems();
  158. }
  159.  
  160. public boolean deleteUser(Principal principal) {
  161. if (findLoggedInUser(principal) != null) {
  162. userRepository.delete(findLoggedInUser(principal));
  163. return true;
  164. }
  165. return false;
  166. }
  167.  
  168. private User findUserbyUsername(String username) {
  169. return userRepository.findByUsername(username);
  170. }
  171.  
  172. public User findLoggedInUser(Principal principal) {
  173. return findUserbyUsername(principal.getName());
  174. }
  175. }
  176.  
  177. @RestController
  178. public class ToDoItemController {
  179. private final ToDoItemServiceBean toDoItemService;
  180. private final UserServiceBean userService;
  181. private final ObjectMapper mapper;
  182.  
  183. @Autowired
  184. public ToDoItemController(ToDoItemServiceBean toDoItemService, UserServiceBean userService, ObjectMapper mapper) {
  185. this.toDoItemService = toDoItemService;
  186. this.userService = userService;
  187. this.mapper = mapper;
  188. }
  189.  
  190. @GetMapping("/todos")
  191. public ResponseEntity viewToDos(Principal principal) {
  192. ObjectNode jsonObject = mapper.createObjectNode();
  193. User currentUser = userService.findLoggedInUser(principal);
  194. if (userService.getAllToDoItems(currentUser) != null) {
  195. return new ResponseEntity<>(userService.getAllToDoItems(currentUser), HttpStatus.OK);
  196. } else {
  197. jsonObject.put("status", "You haven't added any ToDos yet");
  198. return new ResponseEntity<>(jsonObject, HttpStatus.NO_CONTENT);
  199. }
  200. }
  201.  
  202. // CREATE NEW TODOITEM FROM SENT JSON
  203. @PostMapping("/todos")
  204. @ResponseStatus(HttpStatus.CREATED)
  205. public ToDoItem newToDo(
  206. @RequestBody ToDoItem toDoItem,
  207. Principal principal
  208. ) {
  209. User currentUser = userService.findLoggedInUser(principal);
  210. return toDoItemService.addToDo(toDoItem, currentUser);
  211. }
  212.  
  213. @DeleteMapping("/todos/{id}")
  214. public ResponseEntity deleteToDo(
  215. @PathVariable("id") Long itemId,
  216. Principal principal
  217. ) {
  218. ObjectNode jsonObject = mapper.createObjectNode();
  219. User currentUser = userService.findLoggedInUser(principal);
  220. if (toDoItemService.toDoExists(itemId)) {
  221. ToDoItem toDoFromDb = toDoItemService.findToDoItemById(itemId);
  222. if (toDoItemService.canUserAccessToDo(toDoFromDb, currentUser)) {
  223. toDoItemService.deleteToDo(itemId);
  224. return new ResponseEntity(HttpStatus.NO_CONTENT);
  225. } else {
  226. jsonObject.put("status", "You can only delete your ToDos");
  227. return new ResponseEntity<>(jsonObject, HttpStatus.FORBIDDEN);
  228. }
  229. } else {
  230. jsonObject.put("status", "ToDo with that ID doesn't exist.");
  231. return new ResponseEntity<>(jsonObject, HttpStatus.NOT_FOUND);
  232. }
  233. }
  234.  
  235. @PutMapping("/todos/{id}")
  236. public ResponseEntity editToDo(
  237. @PathVariable("id") Long itemId,
  238. @RequestBody ToDoItem newToDoItem,
  239. Principal principal
  240. ) {
  241. ObjectNode jsonObject = mapper.createObjectNode();
  242. User currentUser = userService.findLoggedInUser(principal);
  243. if (toDoItemService.toDoExists(itemId)) {
  244. ToDoItem toDoFromDb = toDoItemService.findToDoItemById(itemId);
  245. if (toDoItemService.canUserAccessToDo(toDoFromDb, currentUser)) {
  246. toDoItemService.editToDo(newToDoItem, toDoFromDb);
  247. return new ResponseEntity<>(newToDoItem, HttpStatus.OK);
  248. } else {
  249. jsonObject.put("status", "You can only edit your ToDos");
  250. return new ResponseEntity<>(jsonObject, HttpStatus.FORBIDDEN);
  251. }
  252. } else {
  253. jsonObject.put("status", "ToDo with that ID doesn't exist.");
  254. return new ResponseEntity<>(jsonObject, HttpStatus.NOT_FOUND);
  255. }
  256. }
  257.  
  258. @PatchMapping("/todos/{id}/complete")
  259. public ResponseEntity editToDo(
  260. @PathVariable("id") Long itemId,
  261. Principal principal
  262. ) {
  263. ObjectNode jsonObject = mapper.createObjectNode();
  264. User currentUser = userService.findLoggedInUser(principal);
  265. if (toDoItemService.toDoExists(itemId)) {
  266. ToDoItem toDoFromDb = toDoItemService.findToDoItemById(itemId);
  267. if (toDoItemService.canUserAccessToDo(toDoFromDb, currentUser)) {
  268. toDoItemService.completeToDo(toDoFromDb);
  269. return new ResponseEntity<>(toDoFromDb, HttpStatus.OK);
  270. } else {
  271. jsonObject.put("status", "You can only complete your ToDos");
  272. return new ResponseEntity<>(jsonObject, HttpStatus.FORBIDDEN);
  273. }
  274. } else {
  275. jsonObject.put("status", "ToDo with that ID doesn't exist.");
  276. return new ResponseEntity<>(jsonObject, HttpStatus.NOT_FOUND);
  277. }
  278. }
  279. }
  280.  
  281. @RestController
  282. public class UserController {
  283. private final UserServiceBean userService;
  284. private final ObjectMapper mapper;
  285.  
  286. @Autowired
  287. public UserController(UserServiceBean userService, ObjectMapper objectMapper) {
  288. this.userService = userService;
  289. this.mapper = objectMapper;
  290. }
  291.  
  292. // CREATE A USER
  293. @PostMapping("/users")
  294. public ResponseEntity<ObjectNode> createUser(@RequestBody User user) {
  295. ObjectNode jsonObject = mapper.createObjectNode();
  296. if (userService.userExists(user)) {
  297. jsonObject.put("status", "User with that username already exists.");
  298. return new ResponseEntity<>(jsonObject, HttpStatus.BAD_REQUEST);
  299. }
  300. userService.saveUser(user);
  301. jsonObject.put("status", "User created.");
  302. return new ResponseEntity<>(jsonObject, HttpStatus.CREATED);
  303. }
  304.  
  305. // DELETE YOUR ACCOUNT - deletes logged in user
  306. @DeleteMapping("/users")
  307. public ResponseEntity deleteUser(Principal principal) {
  308. if (userService.deleteUser(principal)) {
  309. return new ResponseEntity(HttpStatus.NO_CONTENT);
  310. }
  311. return new ResponseEntity(HttpStatus.BAD_REQUEST);
  312. }
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement