source: Git/src/main/java/com/wediscussmovies/project/web/controller/UsersController.java@ 2efe93e

main
Last change on this file since 2efe93e was 2efe93e, checked in by Petar Partaloski <ppartaloski@…>, 2 years ago

Improved Front-End, added card view of movies

  • Property mode set to 100644
File size: 3.1 KB
Line 
1package com.wediscussmovies.project.web.controller;
2
3
4import com.wediscussmovies.project.LoggedUser;
5import com.wediscussmovies.project.model.Movie;
6import com.wediscussmovies.project.model.exception.InvalidArgumentsException;
7import com.wediscussmovies.project.model.exception.PasswordsDoNotMatchException;
8import com.wediscussmovies.project.service.MovieService;
9import com.wediscussmovies.project.service.UserService;
10import org.springframework.stereotype.Controller;
11import org.springframework.ui.Model;
12import org.springframework.web.bind.annotation.GetMapping;
13import org.springframework.web.bind.annotation.PostMapping;
14import org.springframework.web.bind.annotation.RequestMapping;
15import org.springframework.web.bind.annotation.RequestParam;
16
17import java.util.ArrayList;
18import java.util.List;
19
20@Controller
21@RequestMapping
22public class UsersController{
23 private final UserService userService;
24 private final MovieService movieService;
25
26 public UsersController(UserService userService, MovieService movieService) {
27 this.userService = userService;
28 this.movieService = movieService;
29 }
30 @PostMapping("/register")
31 public String register(@RequestParam String username,
32 @RequestParam String password,
33 @RequestParam String repeatedPassword,
34 @RequestParam String email,
35 @RequestParam String name,
36 @RequestParam String surname) {
37 try{
38 this.userService.register(email,username,password,repeatedPassword,name,surname);
39 return "redirect:/login";
40 } catch (InvalidArgumentsException | PasswordsDoNotMatchException exception) {
41 return "redirect:/register?error=" + exception.getMessage();
42 }
43 }
44 @GetMapping("/register")
45 public String getRegisterPage(@RequestParam(required = false) String error, Model model) {
46 addModelError(model,error);
47 model.addAttribute("contentTemplate","register");
48 return "template";
49 }
50 @GetMapping("/login")
51 public String getLoginPage(@RequestParam(required = false) String error,Model model){
52 addModelError(model,error);
53 model.addAttribute("contentTemplate","login");
54 return "template";
55 }
56
57 private void addModelError(Model model,String error){
58 if(error != null && !error.isEmpty()) {
59 model.addAttribute("hasError", true);
60 model.addAttribute("error", error);
61 }
62 }
63 @GetMapping("/favoriteList")
64 public String getFavoriteList(Model model){
65 List<Movie> movieList = this.movieService.findLikedMoviesByUser(LoggedUser.getLoggedUser());
66 List<List<Movie>> movie_rows = new ArrayList<>();
67 for(int i=0; i<movieList.size(); i+=4){
68 int j = i+4;
69 if(j>movieList.size())
70 j= movieList.size();
71 movie_rows.add(movieList.subList(i, j));
72 }
73 model.addAttribute("movie_rows", movie_rows);
74 model.addAttribute("contentTemplate","favoriteList");
75 return "template";
76
77 }
78
79
80}
Note: See TracBrowser for help on using the repository browser.