Changeset 5444409 for src/main


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
Files:
1 added
9 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")
  • src/main/resources/templates/login.html

    r17a2fda r5444409  
    1 
     1<div class="container">
     2    <h1 th:if="${hasError}" th:text="${error}"></h1>
     3</div>
    24<div>
    35    <form th:method="POST" th:action="@{/login}">
  • src/main/resources/templates/register.html

    r17a2fda r5444409  
    44<div class="container">
    55    <form class="form-signin mt-xl-5" method="post" action="/register">
    6         <h2 class="form-signin-heading">Register</h2>
     6        <h2 class="form-signin-heading" style="color: white">Регистрација</h2>
    77        <p>
    8             <label for="username" class="sr-only">Корисничко име</label>
     8            <label for="username" class="sr-only" style="color: white">Корисничко име</label>
    99            <input type="text" id="username" name="username" class="form-control" placeholder="Корисничко име" required=""
    1010                   autofocus="">
    1111        </p>
    1212        <p>
    13             <label for="password" class="sr-only">Лозинка</label>
     13            <label for="password" class="sr-only" style="color: white">Лозинка</label>
    1414            <input type="password" id="password" name="password" class="form-control" placeholder="Лозинка"
    1515                   required="">
    1616        </p>
    1717        <p>
    18             <label for="repeatedPassword" class="sr-only">Повтори лозинка</label>
     18            <label for="repeatedPassword" class="sr-only" style="color: white">Повтори лозинка</label>
    1919            <input type="password" id="repeatedPassword" name="repeatedPassword" class="form-control"
    2020                   placeholder="Повтори лозинка" required="">
    2121        </p>
    2222        <p>
    23             <label for="first_name" class="sr-only">Име</label>
     23            <label for="first_name" class="sr-only" style="color: white">Име</label>
    2424            <input type="text" id="first_name" name="first_name" class="form-control" placeholder="Име" required="" autofocus="">
    2525        </p>
    2626        <p>
    27             <label for="last_name" class="sr-only">Презиме</label>
     27            <label for="last_name" class="sr-only" style="color: white">Презиме</label>
    2828            <input type="text" id="last_name" name="last_name" class="form-control" placeholder="Презиме" required=""
    2929                   autofocus="">
    3030        </p>
    3131        <p>
    32             <label for="number" class="sr-only">Телефонски број</label>
     32            <label for="last_name" class="sr-only" style="color: white">Е-пошта</label>
     33            <input type="text" id="email" name="email" class="form-control" placeholder="Е-пошта" required=""
     34                   autofocus="">
     35        </p>
     36        <p>
     37            <label for="number" class="sr-only" style="color: white">Телефонски број</label>
    3338            <input type="text" id="number" name="number" class="form-control" placeholder="Телефонски број" required=""
    3439                   autofocus="">
     
    3742        <div class="form-check form-check-inline">
    3843            <input class="form-check-input" name="role" type="radio" id="ROLE_ADMIN" value="ROLE_ADMIN">
    39             <label class="form-check-label" for="ROLE_ADMIN">ROLE_ADMIN</label>
     44            <label class="form-check-label" for="ROLE_ADMIN" style="color: white">ROLE_ADMIN</label>
    4045        </div>
    4146        <div class="form-check form-check-inline">
    4247            <input class="form-check-input" name="role"  type="radio" id="ROLE_USER" value="ROLE_USER">
    43             <label class="form-check-label" for="ROLE_USER">ROLE_USER</label>
     48            <label class="form-check-label" for="ROLE_USER" style="color: white">ROLE_USER</label>
    4449        </div>
    4550        </p>
Note: See TracChangeset for help on using the changeset viewer.