Changeset e097c1c for src


Ignore:
Timestamp:
02/04/23 19:28:58 (22 months ago)
Author:
milamihajlovska <mila.mihajlovska01@…>
Branches:
master
Children:
a554435
Parents:
6032d44
Message:

login and register in HomeController

Location:
src/main/java/com/example/moviezone
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/example/moviezone/service/Impl/UserServiceImpl.java

    r6032d44 re097c1c  
    44import com.example.moviezone.model.User;
    55import com.example.moviezone.model.Worker;
     6import com.example.moviezone.model.exceptions.PasswordsDoNotMatchException;
    67import com.example.moviezone.model.exceptions.UserNotFoundException;
    78import com.example.moviezone.repository.UserRepository;
     
    3940
    4041    @Override
    41     public User register(String first_name, String last_name, String username, String email, String number, String password, String role) {
     42    public User register(String first_name, String last_name, String username, String email, String number, String password,String repeatedPassword, String role) {
     43       if(!password.equals(repeatedPassword))
     44           throw new PasswordsDoNotMatchException();
     45       else
    4246        if(role.equals("worker"))
    4347        {
  • src/main/java/com/example/moviezone/service/UserService.java

    r6032d44 re097c1c  
    1010    User findByUsername(String username);
    1111
    12   User register(String first_name, String last_name,String username, String email, String number, String password, String role);
     12  User register(String first_name, String last_name,String username, String email, String number, String password,String repeatedPassword, String role);
    1313    User login(String username,String password);
    1414}
  • src/main/java/com/example/moviezone/web/HomeController.java

    r6032d44 re097c1c  
    22
    33
     4import com.example.moviezone.model.User;
     5import com.example.moviezone.model.exceptions.UserNotFoundException;
    46import com.example.moviezone.service.FilmService;
     7import com.example.moviezone.service.UserService;
    58import org.springframework.stereotype.Controller;
    69import org.springframework.ui.Model;
    710import org.springframework.web.bind.annotation.GetMapping;
     11import org.springframework.web.bind.annotation.PostMapping;
    812import org.springframework.web.bind.annotation.RequestMapping;
     13import org.springframework.web.bind.annotation.RequestParam;
     14
     15import javax.servlet.http.HttpSession;
    916
    1017@Controller
     
    1320
    1421private final FilmService filmService;
     22private final UserService userService;
    1523
    16     public HomeController(FilmService filmService) {
     24    public HomeController(FilmService filmService, UserService userService) {
    1725        this.filmService = filmService;
     26        this.userService = userService;
    1827    }
    1928
     
    2433    }
    2534
    26    
     35    @GetMapping("/login")
     36    public String getLoginPage(Model model)
     37    {
     38        model.addAttribute("bodyContent", "login");
     39        return "master-template";
     40    }
     41
     42    @GetMapping("/register")
     43    public String getRegisterPage(Model model)
     44    {
     45        model.addAttribute("bodyContent", "register");
     46        return "master-template";
     47    }
     48
     49    @PostMapping("/login")
     50    public String login(@RequestParam String username,@RequestParam String password,Model model, HttpSession session)
     51    {
     52        User user = null;
     53        try {
     54            user=userService.login(username,password);
     55            session.setAttribute("sessionUser",user);
     56            model.addAttribute("user",user);
     57            return "redirect:/home";
     58
     59        }catch (UserNotFoundException e)
     60        {
     61            model.addAttribute("hasError", true);
     62            model.addAttribute("error", e.getMessage());
     63            return "login";
     64        }
     65
     66    }
     67
     68    @PostMapping("register")
     69    public String register(@RequestParam String username, @RequestParam String first_name, @RequestParam String last_name,
     70                         @RequestParam String password, @RequestParam String repeatedPassword,
     71                         @RequestParam String email, @RequestParam String number,
     72                         @RequestParam String role)
     73    {
     74        User user = null;
     75            user=userService.register(first_name,last_name,username,email,number,password,repeatedPassword,role);
     76            return "redirect:/login";
     77    }
    2778
    2879}
Note: See TracChangeset for help on using the changeset viewer.