Changeset 6791e89 for src/main/java/mk


Ignore:
Timestamp:
09/22/22 21:16:07 (2 years ago)
Author:
Leona <leona@…>
Branches:
master
Parents:
3692f0d
Message:

working on reservations part

Location:
src/main/java/mk/ukim/finki/db/library
Files:
1 added
10 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/mk/ukim/finki/db/library/model/Book.java

    r3692f0d r6791e89  
    7474    private List<Writer> writers;
    7575
     76    public Long getId() {
     77        return id;
     78    }
     79
    7680    public Library getLibrary() {
    7781        return library;
  • src/main/java/mk/ukim/finki/db/library/model/ReservationBook.java

    r3692f0d r6791e89  
    1818    }
    1919
    20     public ReservationBook(Member member, Book book, Date takeDate, Date returnDate, boolean isReservedBook) {
    21         this.member = member;
    22         this.book = book;
     20    public ReservationBook( Date takeDate, Date returnDate) { //Long id,
     21        //(Long) this.book.getId() = id;
    2322        this.takeDate = takeDate;
    2423        this.returnDate = returnDate;
    25         this.isReservedBook = isReservedBook;
    2624    }
    2725
  • src/main/java/mk/ukim/finki/db/library/repository/MemberRepository.java

    r3692f0d r6791e89  
    11package mk.ukim.finki.db.library.repository;
    22
     3import mk.ukim.finki.db.library.model.Book;
     4import mk.ukim.finki.db.library.model.Member;
    35import org.springframework.stereotype.Repository;
     6
     7import java.util.List;
    48
    59@Repository
  • src/main/java/mk/ukim/finki/db/library/repository/UserRepository.java

    r3692f0d r6791e89  
    66import org.springframework.stereotype.Repository;
    77
     8import java.util.List;
    89import java.util.Optional;
    910
     
    1314
    1415    Optional<Person> findByEmailAndPassword(String email, String password);
     16
     17    List<Person> findByEmail(String email);
    1518}
  • src/main/java/mk/ukim/finki/db/library/service/ReservationBookService.java

    r3692f0d r6791e89  
    1010public interface ReservationBookService {
    1111
    12     ReservationBook findById(Long id);
     12    //ReservationBook getActiveResBookUser(String username);
    1313
    1414    List<ReservationBook> listAll();
    1515
    16     ReservationBook reserveBook(Long memberId, Long bookId, Date takeDate, Date returnDate, boolean isReservedBook);
     16    ReservationBook reserveBook(Long bookId, Date takeDate, Date returnDate);
    1717}
  • src/main/java/mk/ukim/finki/db/library/service/UserService.java

    r3692f0d r6791e89  
    22
    33import mk.ukim.finki.db.library.model.Person;
     4
     5import java.util.List;
    46
    57public interface UserService {
     
    1012    Person login(String username, String password);
    1113
     14    List<Person> findByEmail(String email);
     15
    1216}
  • src/main/java/mk/ukim/finki/db/library/service/impl/ReservationBookServiceImpl.java

    r3692f0d r6791e89  
    22
    33import mk.ukim.finki.db.library.model.*;
    4 import mk.ukim.finki.db.library.model.exception.InvalidReservationBookIdException;
     4import mk.ukim.finki.db.library.model.exception.MemberNotFoundException;
    55import mk.ukim.finki.db.library.repository.BookRepository;
    66import mk.ukim.finki.db.library.repository.MemberRepository;
    77import mk.ukim.finki.db.library.repository.ReservationBookRepository;
     8import mk.ukim.finki.db.library.repository.UserRepository;
    89import mk.ukim.finki.db.library.service.ReservationBookService;
    910import org.springframework.stereotype.Service;
     
    1617
    1718    private final ReservationBookRepository reservationBookRepository;
    18     private final MemberRepository memberRepository;
     19    private final UserRepository userRepository;
    1920    private final BookRepository bookRepository;
    2021
    2122    public ReservationBookServiceImpl(ReservationBookRepository reservationBookRepository, MemberRepository memberRepository, BookRepository bookRepository) {
    2223        this.reservationBookRepository = reservationBookRepository;
    23         this.memberRepository = memberRepository;
     24        this.userRepository = memberRepository;
    2425        this.bookRepository = bookRepository;
    25     }
    26 
    27 
    28     @Override
    29     public ReservationBook findById(Long id) {
    30         return this.reservationBookRepository.findById(id).orElseThrow(InvalidReservationBookIdException::new);
    3126    }
    3227
     
    3732
    3833    @Override
    39     public ReservationBook reserveBook(Long memberId, Long bookId, Date takeDate, Date returnDate, boolean isReservedBook) {
    40         Person member = memberId!=null ? this.memberRepository.findById(memberId).orElse((Member) null) : null;
    41         Book book = bookId!=null ? this.bookRepository.findById(bookId).orElse((Book) null) : null;
     34    public ReservationBook reserveBook(Long bookId, Date takeDate, Date returnDate) {
    4235
    43         ReservationBook reservationBook = new ReservationBook((Member) member, book, takeDate, returnDate, isReservedBook);
    44         //TODO: treba da se zemat site izdanija od knigata, da se proveri dali ima slobodna i ako ima, od vkupnoto da se namali za edno
     36
     37        ReservationBook reservationBook = new ReservationBook(takeDate, returnDate); //bookId,
    4538
    4639        return this.reservationBookRepository.save(reservationBook);
    4740    }
     41
     42
     43
     44//    @Override
     45//    public ReservationBook reserveBook(Long memberId, Long bookId, Date takeDate, Date returnDate, boolean isReservedBook) {
     46//        Person member = memberId!=null ? this.userRepository.findById(memberId).orElse((Member) null) : null;
     47//        Book book = bookId!=null ? this.bookRepository.findById(bookId).orElse((Book) null) : null;
     48//
     49//        ReservationBook reservationBook = new ReservationBook((Member) member, book, takeDate, returnDate, isReservedBook);
     50//        //TODO: treba da se zemat site izdanija od knigata, da se proveri dali ima slobodna i ako ima, od vkupnoto da se namali za edno
     51//
     52//        return this.reservationBookRepository.save(reservationBook);
     53//    }
     54
    4855}
  • src/main/java/mk/ukim/finki/db/library/service/impl/UserServiceImpl.java

    r3692f0d r6791e89  
    88import mk.ukim.finki.db.library.service.UserService;
    99import org.springframework.stereotype.Service;
     10
     11import java.util.List;
    1012
    1113
     
    3537        return userRepository.findByEmailAndPassword(email, password).orElseThrow(InvalidUserCredentialsException::new);
    3638    }
     39
     40    @Override
     41    public List<Person> findByEmail(String email) {
     42        return this.userRepository.findByEmail(email);
     43    }
    3744}
  • src/main/java/mk/ukim/finki/db/library/web/BookController.java

    r3692f0d r6791e89  
    4040
    4141        List<Book> books;
    42         //List<Writer> writers;
     42//        List<Writer> writers;
    4343        //List<Genre> genres;
    4444        //List<Library> libraries;
     
    4646        if(bookName == null){
    4747            books = this.bookService.listAllBooks();
    48            // writers = this.writerService.listAll();
     48//            writers = this.writerService.listAll();
    4949           // genres = this.genreService.listAll();
    5050
     
    6262        return "book.html";
    6363    }
     64
     65
     66
     67
    6468}
  • src/main/java/mk/ukim/finki/db/library/web/ReservationBookController.java

    r3692f0d r6791e89  
    11package mk.ukim.finki.db.library.web;
    22
     3import mk.ukim.finki.db.library.model.exception.InvalidArgumentsException;
     4import mk.ukim.finki.db.library.model.exception.PasswordsDoNotMatchException;
     5import mk.ukim.finki.db.library.service.ReservationBookService;
    36import org.springframework.stereotype.Controller;
    4 import org.springframework.web.bind.annotation.RequestMapping;
     7import org.springframework.ui.Model;
     8import org.springframework.web.bind.annotation.*;
     9
     10import java.util.Date;
    511
    612@Controller
     
    814public class ReservationBookController {
    915
     16    private final ReservationBookService reservationBookService;
     17
     18    public ReservationBookController(ReservationBookService reservationBookService) {
     19        this.reservationBookService = reservationBookService;
     20    }
     21
     22    @GetMapping("/resBook")
     23    public String showRes(){
     24        return "resBook";
     25    }
     26
     27    @GetMapping
     28    public String getReservationBook(@RequestParam(required = false) String error, Model model) {
     29        if(error != null && !error.isEmpty()) {
     30            model.addAttribute("hasError", true);
     31            model.addAttribute("error", error);
     32        }
     33        //ReservationBook reservationBook = this.reservationBookService.getCurrentUserId(id);
     34        model.addAttribute("books", this.reservationBookService.listAll());
     35        return "resBook";
     36    }
     37
     38    @PostMapping("/reserve/{id}")
     39    public String reserve(@PathVariable Long id,
     40                            @RequestParam Date takeDate,
     41                           @RequestParam Date returnDate
     42    ) {
     43        try{
     44            this.reservationBookService.reserveBook(id, takeDate, returnDate);
     45            return "redirect:/book";
     46        } catch (InvalidArgumentsException | PasswordsDoNotMatchException exception) {
     47            return "redirect:/resBook?error=" + exception.getMessage();
     48        }
     49    }
    1050
    1151}
Note: See TracChangeset for help on using the changeset viewer.