Changeset 5444409 for src/main/java


Ignore:
Timestamp:
02/09/23 16:05:47 (23 months ago)
Author:
milamihajlovska <mila.mihajlovska01@…>
Branches:
master
Children:
01a1ca6
Parents:
17a2fda
Message:

update register and registerWorker

Location:
src/main/java/com/example/moviezone
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/example/moviezone/config/CustomUsernamePasswordAuthenticationProvider.java

    r17a2fda r5444409  
    22
    33
     4import com.example.moviezone.model.exceptions.UserNotFoundException;
    45import com.example.moviezone.service.UserService;
    56import org.springframework.security.authentication.AuthenticationProvider;
     
    3536
    3637        UserDetails userDetails = this.userService.findByUsername(username);
     38
    3739//        String realPassword = userDetails.getPassword();
    3840        if (!Objects.equals(password,userDetails.getPassword())) {
  • src/main/java/com/example/moviezone/model/Worker.java

    r17a2fda r5444409  
    3232        super(password, first_name, last_name, address, contact_number, username);
    3333    }
     34    public Worker(String password, String first_name, String last_name, String address, String contact_number, String username,String position,String work_hours_from,String work_hours_to,Cinema cinema) {
     35        super(password, first_name, last_name, address, contact_number, username);
     36        this.position=position;
     37        this.work_hours_from=work_hours_from;
     38        this.work_hours_to=work_hours_to;
     39        this.cinema=cinema;
     40    }
    3441
    3542    @Override
  • src/main/java/com/example/moviezone/service/CinemaService.java

    r17a2fda r5444409  
    77public interface CinemaService {
    88    List<Cinema> findAllCinemas();
     9    Cinema findCinemaById(Integer id_cinema);
    910}
  • src/main/java/com/example/moviezone/service/Impl/CinemaServiceImpl.java

    r17a2fda r5444409  
    2020        return cinemaRepository.findAll();
    2121    }
     22
     23    @Override
     24    public Cinema findCinemaById(Integer id_cinema) {
     25        return cinemaRepository.findById(id_cinema).orElseThrow(RuntimeException::new);
     26    }
    2227}
  • src/main/java/com/example/moviezone/service/Impl/UserServiceImpl.java

    r17a2fda r5444409  
    11package com.example.moviezone.service.Impl;
    22
    3 import com.example.moviezone.model.Customer;
    4 import com.example.moviezone.model.Role;
    5 import com.example.moviezone.model.User;
    6 import com.example.moviezone.model.Worker;
     3import com.example.moviezone.model.*;
    74import com.example.moviezone.model.exceptions.InvalidUsernameOrPasswordException;
    85import com.example.moviezone.model.exceptions.PasswordsDoNotMatchException;
     
    7572    }
    7673
     74    @Override
     75    public void registerWorker(String first_name, String last_name, String username, String email, String number, String password, String position, String work_hours_from, String work_hours_to, Cinema cinema) {
     76        userRepository.save(new Worker(password,first_name,last_name,email,number,username,position,work_hours_from,work_hours_to,cinema));
     77
     78    }
     79
    7780
    7881}
  • src/main/java/com/example/moviezone/service/UserService.java

    r17a2fda r5444409  
    11package com.example.moviezone.service;
    22
     3import com.example.moviezone.model.Cinema;
    34import com.example.moviezone.model.Role;
    45import com.example.moviezone.model.User;
     
    1314    void register(String first_name, String last_name,String username, String email, String number, String password, Role role);
    1415    User login(String username,String password);
     16    void registerWorker(String first_name, String last_name, String username, String email, String number, String password, String position, String work_hours_from, String work_hours_to, Cinema cinema);
    1517}
  • src/main/java/com/example/moviezone/web/HomeController.java

    r17a2fda r5444409  
    1717
    1818import javax.servlet.http.HttpServletRequest;
     19import javax.servlet.http.HttpServletResponse;
    1920import javax.servlet.http.HttpSession;
    2021import javax.transaction.Transactional;
     22import java.io.IOException;
    2123import java.time.LocalDate;
    2224import java.util.LinkedList;
    2325import java.util.List;
     26import java.util.Objects;
    2427import java.util.stream.Collectors;
    2528
     
    151154            model.addAttribute("hasError", true);
    152155            model.addAttribute("error", e.getMessage());
    153             return "/login";
    154         }
    155 
    156     }
    157 
    158     @PostMapping()
    159     public String register(@RequestParam String username,
     156            return "login";
     157        }
     158
     159    }
     160
     161    @PostMapping("/register")
     162    public void register(@RequestParam String username,
    160163                           @RequestParam String first_name,
    161164                           @RequestParam String last_name,
     
    164167                           @RequestParam String email,
    165168                           @RequestParam String number,
    166                            @RequestParam Role role)
    167     {
     169                           @RequestParam Role role,HttpServletResponse response, HttpSession session) throws IOException {
     170
     171        System.out.println(username + first_name+ last_name + password + repeatedPassword + email + number + role);
     172        if(role.equals(Role.ROLE_ADMIN)){
     173            session.setAttribute("username", username);
     174            session.setAttribute("first_name", first_name);
     175            session.setAttribute("last_name", last_name);
     176            session.setAttribute("password", password);
     177            session.setAttribute("repeatedPassword", repeatedPassword);
     178            session.setAttribute("email", email);
     179            session.setAttribute("number", number);
     180            response.sendRedirect("/registerWorker");
     181        }
     182        else {
     183            try {
     184                userService.register(first_name,last_name,username,email,number,password,role);
     185                response.sendRedirect( "redirect:/login");
     186            }catch (PasswordsDoNotMatchException exception)
     187            {
     188//                return "redirect:/register?error=" + exception.getMessage();
     189            }
     190        }
     191
     192    }
     193    @GetMapping("/registerWorker")
     194    public String getRegisterWorkerPage(Model model){
     195        model.addAttribute("cinemas",cinemaService.findAllCinemas());
     196        model.addAttribute("bodyContent","registerWorker");
     197        return "master-template";
     198    }
     199    @PostMapping("/finishRegister")
     200    public void handleWorkerRegister(Model model, HttpServletResponse response, HttpSession session,
     201                                     @RequestParam String position, @RequestParam String work_hours_from,
     202                                     @RequestParam String work_hours_to,@RequestParam Integer id_cinema){
     203        System.out.println("here?");
     204        String username = (String) session.getAttribute("username");
     205        String first_name = (String) session.getAttribute("first_name");
     206        String last_name = (String) session.getAttribute("last_name");
     207        String password = (String) session.getAttribute("password");
     208        String email = (String) session.getAttribute("email");
     209        String number = (String) session.getAttribute("number");
     210        Cinema cinema=cinemaService.findCinemaById(id_cinema);
     211        userService.registerWorker(first_name,last_name,username,email,number,password,position,work_hours_from,work_hours_to,cinema);
    168212        try {
    169            userService.register(first_name,last_name,username,email,number,password,role);
    170             return "redirect:/login";
    171         }catch (PasswordsDoNotMatchException exception)
    172         {
    173             return "redirect:/register?error=" + exception.getMessage();
    174         }
    175 
    176     }
    177 
     213            response.sendRedirect("/login");
     214        } catch (IOException e) {
     215            throw new RuntimeException(e);
     216        }
     217    }
    178218
    179219    @GetMapping("/films")
Note: See TracChangeset for help on using the changeset viewer.