From 4c8cc047a0af39c0513a555242077c93da745bd4 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 09:33:13 -0700 Subject: [PATCH 01/35] Create Section.java working on models --- .../main/java/com/lambdaschool/usermodel/models/Section.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java new file mode 100644 index 0000000..a3b21a6 --- /dev/null +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java @@ -0,0 +1,4 @@ +package com.lambdaschool.usermodel.models; + +public class Section { +} From 41a87eae9523de2fed1a30eb613f2dd2f2a03eb1 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 09:35:46 -0700 Subject: [PATCH 02/35] Update Section.java --- .../usermodel/models/Section.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java index a3b21a6..ae9a3e3 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java @@ -1,4 +1,36 @@ package com.lambdaschool.usermodel.models; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + public class Section { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long sectionid; + + private String name; + + + public Section(){} + public Section(String name) { + this.name = name; + } + + public long getSectionid() { + return sectionid; + } + + public void setSectionid(long sectionid) { + this.sectionid = sectionid; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } } From d1e943eafb1a362197e096f6fcf5bb925171c1bc Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 09:41:33 -0700 Subject: [PATCH 03/35] Author Model --- .../lambdaschool/usermodel/models/Author.java | 49 +++++++++++++++++++ .../usermodel/models/Section.java | 9 ++-- 2 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java new file mode 100644 index 0000000..d42d06e --- /dev/null +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java @@ -0,0 +1,49 @@ +package com.lambdaschool.usermodel.models; + +import com.lambdaschool.usermodel.logging.Loggable; + +import javax.persistence.*; + +@Loggable +@Entity +@Table(name = "authors") +public class Author { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long bookid; + private String fname; + private String lname; + + + public Author() {} + + public Author(String fname, String lname) { + this.fname = fname; + this.lname = lname; + } + + public long getBookid() { + return bookid; + } + + public void setBookid(long bookid) { + this.bookid = bookid; + } + + public String getFname() { + return fname; + } + + public void setFname(String fname) { + this.fname = fname; + } + + public String getLname() { + return lname; + } + + public void setLname(String lname) { + this.lname = lname; + } +} diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java index ae9a3e3..47994a0 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java @@ -1,9 +1,12 @@ package com.lambdaschool.usermodel.models; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; +import com.lambdaschool.usermodel.logging.Loggable; +import javax.persistence.*; + +@Loggable +@Entity +@Table(name = "sections") public class Section { @Id From ccbaa236b5b41c17640a6903b0a274ad5186884b Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 09:47:12 -0700 Subject: [PATCH 04/35] Create Book.java --- .../lambdaschool/usermodel/models/Book.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java new file mode 100644 index 0000000..e068afe --- /dev/null +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java @@ -0,0 +1,61 @@ +package com.lambdaschool.usermodel.models; + +import com.lambdaschool.usermodel.logging.Loggable; + +import javax.persistence.*; + +@Loggable +@Entity +@Table(name = "books") +public class Book { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long bookid; + + private String title; + private String ISBN; + @Column(nullable = true) + private int copy; + + + public Book(){} + + public Book(String title, String ISBN, int copy) { + this.title = title; + this.ISBN = ISBN; + this.copy = copy; + } + + public long getBookid() { + return bookid; + } + + public void setBookid(long bookid) { + this.bookid = bookid; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getISBN() { + return ISBN; + } + + public void setISBN(String ISBN) { + this.ISBN = ISBN; + } + + public int getCopy() { + return copy; + } + + public void setCopy(int copy) { + this.copy = copy; + } +} From c54777edc0ade343fa1d2b3fee467cbb79754d84 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 09:53:53 -0700 Subject: [PATCH 05/35] Create Wrote.java --- .../lambdaschool/usermodel/models/Wrote.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java new file mode 100644 index 0000000..7f693d8 --- /dev/null +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java @@ -0,0 +1,47 @@ +package com.lambdaschool.usermodel.models; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.lambdaschool.usermodel.logging.Loggable; + +import javax.persistence.*; +import java.io.Serializable; + +@Loggable +@Entity +@Table(name = "wrote", + uniqueConstraints = {@UniqueConstraint(columnNames = {"bookid", "authorid"})}) +public class Wrote extends Auditable implements Serializable { + @Id + @ManyToOne + @JoinColumn(name = "bookid") + @JsonIgnoreProperties("wrote") + private Book book; + @Id + @ManyToOne + @JoinColumn(name = "authorid") + @JsonIgnoreProperties("wrote") + private Author author; + + public Wrote(){} + + public Wrote(Book book, Author author) { + this.book = book; + this.author = author; + } + + public Book getBook() { + return book; + } + + public void setBook(Book book) { + this.book = book; + } + + public Author getAuthor() { + return author; + } + + public void setAuthor(Author author) { + this.author = author; + } +} From 07572cc244ab71f95344a88e1ed2a60d86739980 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 09:58:01 -0700 Subject: [PATCH 06/35] Update Section.java --- .../com/lambdaschool/usermodel/models/Section.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java index 47994a0..5eb126d 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java @@ -1,13 +1,16 @@ package com.lambdaschool.usermodel.models; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.lambdaschool.usermodel.logging.Loggable; import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; @Loggable @Entity @Table(name = "sections") -public class Section { +public class Section extends Auditable{ @Id @GeneratedValue(strategy = GenerationType.AUTO) @@ -15,6 +18,11 @@ public class Section { private String name; + @OneToMany(mappedBy = "section", + cascade = CascadeType.ALL) + @JsonIgnoreProperties("section") + private List wrote = new ArrayList<>(); + public Section(){} public Section(String name) { From 93faf9315ecf869921fe43e98c9cc840fb669e4c Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 10:01:17 -0700 Subject: [PATCH 07/35] Update Author.java --- .../com/lambdaschool/usermodel/models/Author.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java index d42d06e..8c8eeea 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java @@ -1,13 +1,16 @@ package com.lambdaschool.usermodel.models; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.lambdaschool.usermodel.logging.Loggable; import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; @Loggable @Entity @Table(name = "authors") -public class Author { +public class Author extends Auditable{ @Id @GeneratedValue(strategy = GenerationType.AUTO) @@ -15,6 +18,16 @@ public class Author { private String fname; private String lname; + @OneToMany(mappedBy = "author", + cascade = CascadeType.ALL) + @JsonIgnoreProperties("author") + private List wrote = new ArrayList<>(); + + @OneToMany(mappedBy = "author", + cascade = CascadeType.ALL, + orphanRemoval = true) + @JsonIgnoreProperties("author") + private List books = new ArrayList<>(); public Author() {} From 8df4001ff1119bfaf85b6428299753f7cf7896e7 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 10:14:46 -0700 Subject: [PATCH 08/35] Update Book.java --- .../src/main/java/com/lambdaschool/usermodel/models/Book.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java index e068afe..8bfe180 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java @@ -7,7 +7,7 @@ @Loggable @Entity @Table(name = "books") -public class Book { +public class Book extends Auditable { @Id @GeneratedValue(strategy = GenerationType.AUTO) From 30356340b7af4d5cda6067538a1b5c61e0d51b9a Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 10:16:50 -0700 Subject: [PATCH 09/35] Create SectionRepo.java --- .../com/lambdaschool/usermodel/repository/SectionRepo.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 oauth2/src/main/java/com/lambdaschool/usermodel/repository/SectionRepo.java diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/repository/SectionRepo.java b/oauth2/src/main/java/com/lambdaschool/usermodel/repository/SectionRepo.java new file mode 100644 index 0000000..f24e403 --- /dev/null +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/repository/SectionRepo.java @@ -0,0 +1,7 @@ +package com.lambdaschool.usermodel.repository; + +import com.lambdaschool.usermodel.models.Section; +import org.springframework.data.repository.CrudRepository; + +public interface SectionRepo extends CrudRepository { +} From 68a9696ba0b73558f9bcf01f2d98b5fc8bb07dcc Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 10:18:24 -0700 Subject: [PATCH 10/35] other repos --- .../com/lambdaschool/usermodel/repository/AuthorRepo.java | 7 +++++++ .../com/lambdaschool/usermodel/repository/BookRepo.java | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 oauth2/src/main/java/com/lambdaschool/usermodel/repository/AuthorRepo.java create mode 100644 oauth2/src/main/java/com/lambdaschool/usermodel/repository/BookRepo.java diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/repository/AuthorRepo.java b/oauth2/src/main/java/com/lambdaschool/usermodel/repository/AuthorRepo.java new file mode 100644 index 0000000..594c4a1 --- /dev/null +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/repository/AuthorRepo.java @@ -0,0 +1,7 @@ +package com.lambdaschool.usermodel.repository; + +import com.lambdaschool.usermodel.models.Author; +import org.springframework.data.repository.CrudRepository; + +public interface AuthorRepo extends CrudRepository { +} diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/repository/BookRepo.java b/oauth2/src/main/java/com/lambdaschool/usermodel/repository/BookRepo.java new file mode 100644 index 0000000..2d54c83 --- /dev/null +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/repository/BookRepo.java @@ -0,0 +1,7 @@ +package com.lambdaschool.usermodel.repository; + +import com.lambdaschool.usermodel.models.Book; +import org.springframework.data.repository.CrudRepository; + +public interface BookRepo extends CrudRepository { +} From c74bc9b5a4a513ab7b74ee028f3f56be175fdd2d Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 10:20:46 -0700 Subject: [PATCH 11/35] Create SectionService.java --- .../usermodel/services/SectionService.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionService.java diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionService.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionService.java new file mode 100644 index 0000000..06b3f09 --- /dev/null +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionService.java @@ -0,0 +1,22 @@ +package com.lambdaschool.usermodel.services; + +import com.lambdaschool.usermodel.models.Role; +import com.lambdaschool.usermodel.models.Section; + +import java.util.List; + +public interface SectionService +{ + List
findAll(); + + Section findSectionById(long id); + + void delete(long id); + + Section save(Section section); + + Section findByName(String name); + + Section update(long id, + Section section); +} From b97e2ed0ef54a22a88fe5aac58c8d943c4f70a04 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 10:34:53 -0700 Subject: [PATCH 12/35] serviceSectionImpl --- .../lambdaschool/usermodel/models/Author.java | 16 +++ .../usermodel/models/Section.java | 8 ++ .../usermodel/repository/SectionRepo.java | 2 + .../services/SectionServiceImpl.java | 100 ++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionServiceImpl.java diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java index 8c8eeea..6fa285a 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java @@ -56,6 +56,22 @@ public String getLname() { return lname; } + public List getWrote() { + return wrote; + } + + public void setWrote(List wrote) { + this.wrote = wrote; + } + + public List getBooks() { + return books; + } + + public void setBooks(List books) { + this.books = books; + } + public void setLname(String lname) { this.lname = lname; } diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java index 5eb126d..d89b067 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java @@ -41,6 +41,14 @@ public String getName() { return name; } + public List getWrote() { + return wrote; + } + + public void setWrote(List wrote) { + this.wrote = wrote; + } + public void setName(String name) { this.name = name; } diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/repository/SectionRepo.java b/oauth2/src/main/java/com/lambdaschool/usermodel/repository/SectionRepo.java index f24e403..5be32a5 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/repository/SectionRepo.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/repository/SectionRepo.java @@ -4,4 +4,6 @@ import org.springframework.data.repository.CrudRepository; public interface SectionRepo extends CrudRepository { + Section findByNameIgnoreCase(String name); + } diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionServiceImpl.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionServiceImpl.java new file mode 100644 index 0000000..5fc2d12 --- /dev/null +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionServiceImpl.java @@ -0,0 +1,100 @@ +package com.lambdaschool.usermodel.services; + +import com.lambdaschool.usermodel.exceptions.ResourceFoundException; +import com.lambdaschool.usermodel.exceptions.ResourceNotFoundException; +import com.lambdaschool.usermodel.logging.Loggable; +import com.lambdaschool.usermodel.models.Role; +import com.lambdaschool.usermodel.models.Section; +import com.lambdaschool.usermodel.repository.AuthorRepo; +import com.lambdaschool.usermodel.repository.RoleRepository; +import com.lambdaschool.usermodel.repository.SectionRepo; +import com.lambdaschool.usermodel.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.List; + +@Loggable +@Service(value = "roleService") +public class SectionServiceImpl implements SectionService +{ + @Autowired + SectionRepo sectionRepo; + + @Autowired + AuthorRepo authorRepo; + + @Autowired + UserAuditing userAuditing; + + @Override + public List
findAll() + { + List
list = new ArrayList<>(); + sectionRepo.findAll() + .iterator() + .forEachRemaining(list::add); + return list; + } + + + @Override + public Section findSectionById(long id) + { + return sectionRepo.findById(id) + .orElseThrow(() -> new ResourceNotFoundException("Section id " + id + " not found!")); + } + + @Override + public Section findByName(String name) + { + Section rr = sectionRepo.findByNameIgnoreCase(name); + + if (rr != null) + { + return rr; + } else + { + throw new ResourceNotFoundException(name); + } + } + + @Transactional + @Override + public void delete(long id) + { + sectionRepo.findById(id) + .orElseThrow(() -> new ResourceNotFoundException("Sction id " + id + " not found!")); + sectionRepo.deleteById(id); + } + + + @Transactional + @Override + public Section update(long id, + Section section) { + if (section.getName() == null) { + throw new ResourceNotFoundException("No section name found to update!"); + } + return section; + } + + + @Transactional + @Override + public Section save(Section section) + { + Section newSection = new Section(); + newSection.setName(section.getName()); + if (section.getWrote() + .size() > 0) + { + throw new ResourceFoundException("User Roles are not updated through Role. See endpoint POST: users/user/{userid}/role/{roleid}"); + } + + return sectionRepo.save(section); + } +} + From 2b2d86368c6b24559ae5aaf60534302cd8dece59 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 10:38:48 -0700 Subject: [PATCH 13/35] Create AuthorService.java --- .../usermodel/services/AuthorService.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorService.java diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorService.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorService.java new file mode 100644 index 0000000..4caec38 --- /dev/null +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorService.java @@ -0,0 +1,30 @@ +package com.lambdaschool.usermodel.services; + +import com.lambdaschool.usermodel.models.Author; +import org.springframework.data.domain.Pageable; + +import java.util.List; + +public interface AuthorService { + List findAll(Pageable pageable); + + List findByNameContaining(String username, + Pageable pageable); + + Author findAuthorById(long id); + + Author findByName(String name); + + void delete(long id); + + Author save(Author author); + + Author update(Author author, + long id); + + void deleteAuthorWrote(long bookid, + long authorid); + + void addAuthorWrote(long bookid, + long authorid); +} From 1750d99178fefb9e372176e5b926279deb99c931 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 11:08:56 -0700 Subject: [PATCH 14/35] AuthorServiceImpl --- .../usermodel/repository/AuthorRepo.java | 3 + .../usermodel/services/AuthorService.java | 16 +---- .../usermodel/services/AuthorServiceImpl.java | 70 +++++++++++++++++++ .../services/SectionServiceImpl.java | 2 +- 4 files changed, 76 insertions(+), 15 deletions(-) create mode 100644 oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorServiceImpl.java diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/repository/AuthorRepo.java b/oauth2/src/main/java/com/lambdaschool/usermodel/repository/AuthorRepo.java index 594c4a1..503cf78 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/repository/AuthorRepo.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/repository/AuthorRepo.java @@ -1,7 +1,10 @@ package com.lambdaschool.usermodel.repository; import com.lambdaschool.usermodel.models.Author; +import com.lambdaschool.usermodel.models.User; import org.springframework.data.repository.CrudRepository; public interface AuthorRepo extends CrudRepository { + User findByUsername(String name); + } diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorService.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorService.java index 4caec38..1d58cc5 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorService.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorService.java @@ -6,25 +6,13 @@ import java.util.List; public interface AuthorService { - List findAll(Pageable pageable); - - List findByNameContaining(String username, - Pageable pageable); + List findAll(); Author findAuthorById(long id); - Author findByName(String name); - void delete(long id); Author save(Author author); - Author update(Author author, - long id); - - void deleteAuthorWrote(long bookid, - long authorid); - - void addAuthorWrote(long bookid, - long authorid); + Author update(long id, Author author); } diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorServiceImpl.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorServiceImpl.java new file mode 100644 index 0000000..02a2160 --- /dev/null +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorServiceImpl.java @@ -0,0 +1,70 @@ +package com.lambdaschool.usermodel.services; + +import com.lambdaschool.usermodel.exceptions.ResourceFoundException; +import com.lambdaschool.usermodel.exceptions.ResourceNotFoundException; +import com.lambdaschool.usermodel.logging.Loggable; +import com.lambdaschool.usermodel.models.*; +import com.lambdaschool.usermodel.repository.AuthorRepo; +import com.lambdaschool.usermodel.repository.BookRepo; +import com.lambdaschool.usermodel.repository.RoleRepository; +import com.lambdaschool.usermodel.repository.UserRepository; +import com.lambdaschool.usermodel.view.UserNameCountEmails; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Pageable; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.List; + +@Loggable +@Service(value = "authorService") +public class AuthorServiceImpl implements AuthorService { + + + @Autowired + AuthorRepo authorRepo; + @Autowired + BookRepo bookRepo; + + @Override + public List findAll() { + List list = new ArrayList<>(); + authorRepo.findAll().iterator().forEachRemaining(list::add); + return list; + } + + @Override + public Author findAuthorById(long id) { + return authorRepo.findById(id).orElseThrow( () -> new ResourceNotFoundException("could not find an author")); + } + + @Override + public void delete(long id) { + authorRepo.findById(id).orElseThrow(() -> new ResourceNotFoundException("nothing to delete")); + authorRepo.deleteById(id); + } + + + @Override + public Author save(Author author) { + Author newAuthor = new Author(); + newAuthor.setFname(author.getFname()); + newAuthor.setLname(author.getLname()); + if (author.getWrote().size() > 0) throw new ResourceNotFoundException("no author found"); + return authorRepo.save(author); + } + + @Override + public Author update(long id, Author author) { + Author currentAuthor = findAuthorById(id); + currentAuthor.setFname(author.getFname()); + currentAuthor.setLname(author.getLname()); + if (author.getWrote().size() > 0) throw new ResourceNotFoundException("no author found"); + return authorRepo.save(author); + } +} + + diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionServiceImpl.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionServiceImpl.java index 5fc2d12..4771462 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionServiceImpl.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionServiceImpl.java @@ -17,7 +17,7 @@ import java.util.List; @Loggable -@Service(value = "roleService") +@Service(value = "sectionService") public class SectionServiceImpl implements SectionService { @Autowired From f786ac612aff2c877c5a41ffaa1e3ad208a8fada Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 11:09:57 -0700 Subject: [PATCH 15/35] Create BookService.java --- .../java/com/lambdaschool/usermodel/services/BookService.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java new file mode 100644 index 0000000..ab1348a --- /dev/null +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java @@ -0,0 +1,4 @@ +package com.lambdaschool.usermodel.services; + +public interface BookService { +} From c5c0e3e729fe6b8da7d3e1254679a141f0a2e776 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 11:11:05 -0700 Subject: [PATCH 16/35] Update BookService.java --- .../usermodel/services/BookService.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java index ab1348a..f4f61a0 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java @@ -1,4 +1,18 @@ package com.lambdaschool.usermodel.services; +import com.lambdaschool.usermodel.models.Book; + +import java.util.List; + public interface BookService { + + List findAll(); + + Book findBookById(long id); + + void delete(long id); + + Book save(Book book); + + Book update(long id, Book book); } From 1042c3fe0789b56661b60ca93012e983a81927bb Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 11:24:11 -0700 Subject: [PATCH 17/35] BookServiceImpl --- .../lambdaschool/usermodel/models/Book.java | 2 +- .../usermodel/services/BookServiceImpl.java | 61 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java index 8bfe180..35539c2 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java @@ -52,7 +52,7 @@ public void setISBN(String ISBN) { } public int getCopy() { - return copy; + return this.copy; } public void setCopy(int copy) { diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java new file mode 100644 index 0000000..e0446ce --- /dev/null +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java @@ -0,0 +1,61 @@ +package com.lambdaschool.usermodel.services; + +import com.lambdaschool.usermodel.exceptions.ResourceNotFoundException; +import com.lambdaschool.usermodel.models.Book; +import com.lambdaschool.usermodel.repository.AuthorRepo; +import com.lambdaschool.usermodel.repository.BookRepo; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.ArrayList; +import java.util.List; + +public class BookServiceImpl implements BookService { + + @Autowired + private BookRepo bookRepo; + @Autowired + private AuthorRepo authorRepo; + + @Override + public List findAll() { + List list = new ArrayList<>(); + bookRepo.findAll().iterator().forEachRemaining(list::add); + return list; + } + + @Override + public Book findBookById(long id) { + return bookRepo.findById(id).orElseThrow(()-> new ResourceNotFoundException("nothing to find by that id")); + + } + + @Override + public void delete(long id) { + bookRepo.findById(id).orElseThrow(() -> new ResourceNotFoundException("nothing to delete")); + bookRepo.deleteById(id); + } + + @Override + public Book save(Book book) { + Book newBook = new Book(); + newBook.setTitle(book.getTitle()); + newBook.setISBN(book.getISBN()); + newBook.setCopy(book.getCopy()); + return bookRepo.save(book); + } + + @Override + public Book update(long id, Book book) { + Book currentBook = findBookById(id); + if (book.getTitle() != null) { + currentBook.setTitle(book.getTitle()); + } + if (book.getISBN() != null) { + currentBook.setISBN(book.getISBN()); + } + + currentBook.getCopy(); + + return bookRepo.save(book); + } +} From 1285c64909c2f1257df9c005f2bd64f7afbc5b01 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 11:33:08 -0700 Subject: [PATCH 18/35] AuthorController there we go, very first end point and its not working --- .../controllers/AuthorController.java | 33 +++++++++++++++++++ .../usermodel/services/BookServiceImpl.java | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 oauth2/src/main/java/com/lambdaschool/usermodel/controllers/AuthorController.java diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/AuthorController.java b/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/AuthorController.java new file mode 100644 index 0000000..75a769e --- /dev/null +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/AuthorController.java @@ -0,0 +1,33 @@ +package com.lambdaschool.usermodel.controllers; + +import com.lambdaschool.usermodel.handlers.RestExceptionHandler; +import com.lambdaschool.usermodel.logging.Loggable; +import com.lambdaschool.usermodel.services.AuthorService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; + +@RestController +@RequestMapping(value = "/authors") +@Loggable +public class AuthorController { + private static final Logger logger = LoggerFactory.getLogger(RestExceptionHandler.class); + @Autowired + private AuthorService authorService; + + // http://localhost:2019/authors/authors + @GetMapping(value = "/authors", produces = {"application/json"}) + public ResponseEntity listAllAuthors(HttpServletRequest request) + { + logger.trace(request.getMethod() + .toUpperCase() + " " + request.getRequestURI() + " accessed"); + return new ResponseEntity<>(authorService.findAll(), HttpStatus.OK); + } +} diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java index e0446ce..9eeca3b 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java @@ -53,7 +53,7 @@ public Book update(long id, Book book) { if (book.getISBN() != null) { currentBook.setISBN(book.getISBN()); } - + currentBook.getCopy(); return bookRepo.save(book); From 5c7a2c6a6d386e81141b934c57c8f8094e78e5f3 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 12:01:05 -0700 Subject: [PATCH 19/35] 1st end point still not working something wrong with the class --- .../lambdaschool/usermodel/models/Author.java | 21 ++++++++----------- .../lambdaschool/usermodel/models/Book.java | 16 ++++++++++++++ .../lambdaschool/usermodel/models/Wrote.java | 19 +++++++++++++++-- .../usermodel/services/AuthorServiceImpl.java | 4 ++-- 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java index 6fa285a..a91d81f 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java @@ -18,16 +18,13 @@ public class Author extends Auditable{ private String fname; private String lname; - @OneToMany(mappedBy = "author", - cascade = CascadeType.ALL) - @JsonIgnoreProperties("author") - private List wrote = new ArrayList<>(); + @ManyToMany(mappedBy = "authors", cascade = CascadeType.ALL) + @JsonIgnoreProperties(value = "authors") + private List books = new ArrayList<>(); - @OneToMany(mappedBy = "author", - cascade = CascadeType.ALL, - orphanRemoval = true) + @OneToMany(mappedBy = "author", cascade = CascadeType.ALL, orphanRemoval = true) @JsonIgnoreProperties("author") - private List books = new ArrayList<>(); + private List writer = new ArrayList<>(); public Author() {} @@ -56,12 +53,12 @@ public String getLname() { return lname; } - public List getWrote() { - return wrote; + public List getWriter() { + return writer; } - public void setWrote(List wrote) { - this.wrote = wrote; + public void setWriter(List writer) { + this.writer = writer; } public List getBooks() { diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java index 35539c2..c01ab77 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java @@ -1,8 +1,11 @@ package com.lambdaschool.usermodel.models; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.lambdaschool.usermodel.logging.Loggable; import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; @Loggable @Entity @@ -18,6 +21,19 @@ public class Book extends Auditable { @Column(nullable = true) private int copy; + @ManyToMany + @JsonIgnoreProperties("books") + private List authors = new ArrayList<>(); + + @ManyToOne + @JoinColumn(name = "sectionid") + @JsonIgnoreProperties("books") + private Section section; + + @OneToMany(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true) + @JsonIgnoreProperties("book") + private List writers = new ArrayList<>(); + public Book(){} diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java index 7f693d8..621b125 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java @@ -5,17 +5,18 @@ import javax.persistence.*; import java.io.Serializable; +import java.util.Objects; @Loggable @Entity -@Table(name = "wrote", - uniqueConstraints = {@UniqueConstraint(columnNames = {"bookid", "authorid"})}) +@Table(name = "wrote") public class Wrote extends Auditable implements Serializable { @Id @ManyToOne @JoinColumn(name = "bookid") @JsonIgnoreProperties("wrote") private Book book; + @Id @ManyToOne @JoinColumn(name = "authorid") @@ -44,4 +45,18 @@ public Author getAuthor() { public void setAuthor(Author author) { this.author = author; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Wrote)) return false; + Wrote wrote = (Wrote) o; + return Objects.equals(getBook(), wrote.getBook()) && + Objects.equals(getAuthor(), wrote.getAuthor()); + } + + @Override + public int hashCode() { + return Objects.hash(getBook(), getAuthor()); + } } diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorServiceImpl.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorServiceImpl.java index 02a2160..1cb8dcf 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorServiceImpl.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorServiceImpl.java @@ -53,7 +53,7 @@ public Author save(Author author) { Author newAuthor = new Author(); newAuthor.setFname(author.getFname()); newAuthor.setLname(author.getLname()); - if (author.getWrote().size() > 0) throw new ResourceNotFoundException("no author found"); + if (author.getWriter().size() > 0) throw new ResourceNotFoundException("no author found"); return authorRepo.save(author); } @@ -62,7 +62,7 @@ public Author update(long id, Author author) { Author currentAuthor = findAuthorById(id); currentAuthor.setFname(author.getFname()); currentAuthor.setLname(author.getLname()); - if (author.getWrote().size() > 0) throw new ResourceNotFoundException("no author found"); + if (author.getWriter().size() > 0) throw new ResourceNotFoundException("no author found"); return authorRepo.save(author); } } From d87918d957ec6e1e3c86d8f678c18f50b96f62a2 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 13:16:45 -0700 Subject: [PATCH 20/35] AuthorController first EndPoint works. only admin can view with token --- .../config/H2ServerConfiguration.java | 4 +-- .../controllers/AuthorController.java | 2 +- .../lambdaschool/usermodel/models/Author.java | 2 ++ .../lambdaschool/usermodel/models/Book.java | 24 +++++++++++++ .../usermodel/models/Section.java | 34 +++++++++---------- .../lambdaschool/usermodel/models/Wrote.java | 18 ++-------- .../usermodel/repository/AuthorRepo.java | 2 +- .../services/SectionServiceImpl.java | 2 +- .../src/main/resources/application.properties | 2 +- 9 files changed, 50 insertions(+), 40 deletions(-) diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/config/H2ServerConfiguration.java b/oauth2/src/main/java/com/lambdaschool/usermodel/config/H2ServerConfiguration.java index 17e48f3..6734947 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/config/H2ServerConfiguration.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/config/H2ServerConfiguration.java @@ -15,11 +15,11 @@ public class H2ServerConfiguration { // TCP port for remote connections, default 9092 - @Value("${h2.tcp.port:9092}") + @Value("${h2.tcp.port:9091}") private String h2TcpPort; // Web port, default 8082 - @Value("${h2.web.port:8082}") + @Value("${h2.web.port:8081}") private String h2WebPort; /** diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/AuthorController.java b/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/AuthorController.java index 75a769e..604b54a 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/AuthorController.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/AuthorController.java @@ -22,7 +22,7 @@ public class AuthorController { @Autowired private AuthorService authorService; - // http://localhost:2019/authors/authors + // http://localhost:2021/authors/authors @GetMapping(value = "/authors", produces = {"application/json"}) public ResponseEntity listAllAuthors(HttpServletRequest request) { diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java index a91d81f..252b602 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java @@ -72,4 +72,6 @@ public void setBooks(List books) { public void setLname(String lname) { this.lname = lname; } + + } diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java index c01ab77..5e9b6d1 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java @@ -43,6 +43,30 @@ public Book(String title, String ISBN, int copy) { this.copy = copy; } + public List getAuthors() { + return authors; + } + + public void setAuthors(List authors) { + this.authors = authors; + } + + public Section getSection() { + return section; + } + + public void setSection(Section section) { + this.section = section; + } + + public List getWriters() { + return writers; + } + + public void setWriters(List writers) { + this.writers = writers; + } + public long getBookid() { return bookid; } diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java index d89b067..97ff380 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Section.java @@ -1,32 +1,30 @@ package com.lambdaschool.usermodel.models; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.lambdaschool.usermodel.logging.Loggable; import javax.persistence.*; import java.util.ArrayList; import java.util.List; -@Loggable @Entity -@Table(name = "sections") -public class Section extends Auditable{ - +@Table(name = "section") +public class Section extends Auditable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long sectionid; + @Column(nullable = false, unique = true) private String name; - @OneToMany(mappedBy = "section", - cascade = CascadeType.ALL) + @OneToMany(mappedBy = "section", cascade = CascadeType.ALL) @JsonIgnoreProperties("section") - private List wrote = new ArrayList<>(); + private List books = new ArrayList<>(); + public Section() { + } - public Section(){} - public Section(String name) { - this.name = name; + public Section(String sectionname) { + this.name = sectionname; } public long getSectionid() { @@ -41,15 +39,15 @@ public String getName() { return name; } - public List getWrote() { - return wrote; + public void setName(String name) { + this.name = name; } - public void setWrote(List wrote) { - this.wrote = wrote; + public List getBooks() { + return books; } - public void setName(String name) { - this.name = name; + public void setBooks(List books) { + this.books = books; } -} +} \ No newline at end of file diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java index 621b125..44020db 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java @@ -1,13 +1,11 @@ package com.lambdaschool.usermodel.models; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.lambdaschool.usermodel.logging.Loggable; import javax.persistence.*; import java.io.Serializable; import java.util.Objects; -@Loggable @Entity @Table(name = "wrote") public class Wrote extends Auditable implements Serializable { @@ -23,7 +21,8 @@ public class Wrote extends Auditable implements Serializable { @JsonIgnoreProperties("wrote") private Author author; - public Wrote(){} + public Wrote() { + } public Wrote(Book book, Author author) { this.book = book; @@ -46,17 +45,4 @@ public void setAuthor(Author author) { this.author = author; } - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof Wrote)) return false; - Wrote wrote = (Wrote) o; - return Objects.equals(getBook(), wrote.getBook()) && - Objects.equals(getAuthor(), wrote.getAuthor()); - } - - @Override - public int hashCode() { - return Objects.hash(getBook(), getAuthor()); - } } diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/repository/AuthorRepo.java b/oauth2/src/main/java/com/lambdaschool/usermodel/repository/AuthorRepo.java index 503cf78..857f3c8 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/repository/AuthorRepo.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/repository/AuthorRepo.java @@ -5,6 +5,6 @@ import org.springframework.data.repository.CrudRepository; public interface AuthorRepo extends CrudRepository { - User findByUsername(String name); + } diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionServiceImpl.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionServiceImpl.java index 4771462..b5fd92a 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionServiceImpl.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionServiceImpl.java @@ -88,7 +88,7 @@ public Section save(Section section) { Section newSection = new Section(); newSection.setName(section.getName()); - if (section.getWrote() + if (section.getBooks() .size() > 0) { throw new ResourceFoundException("User Roles are not updated through Role. See endpoint POST: users/user/{userid}/role/{roleid}"); diff --git a/oauth2/src/main/resources/application.properties b/oauth2/src/main/resources/application.properties index e96fd61..2dc89f9 100644 --- a/oauth2/src/main/resources/application.properties +++ b/oauth2/src/main/resources/application.properties @@ -17,7 +17,7 @@ local.run.db=H2 # postgres://bzhxqmxdgwigkw:611e5353ffd763b09aed8fad07d243e9bbf44e13f8f542330e3b862a1df47c92@ec2-54-221-243-211.compute-1.amazonaws.com:5432/d1blvk8nmshe0v # postgres://cdtpdlmrduoyqv:4958b9a3ba0533fa4fae763ac284bea71ea29f6955b9e36896740bed23289175@ec2-23-21-94-99.compute-1.amazonaws.com:5432/d37hecggdrqi5s # posgress://username :password @url host :5432/dbname -server.port=${PORT:2019} +server.port=${PORT:2021} # Begin h2 configuration spring.h2.console.enabled=true spring.h2.console.path=/h2-console From c4f6a2cd021701c452a1302b20b5a7351e4a1f9b Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 13:17:57 -0700 Subject: [PATCH 21/35] Create AuthorControllerTest.java created my very first test and it passed --- .../usermodel/controllers/AuthorControllerTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 oauth2/src/test/java/com/lambdaschool/usermodel/controllers/AuthorControllerTest.java diff --git a/oauth2/src/test/java/com/lambdaschool/usermodel/controllers/AuthorControllerTest.java b/oauth2/src/test/java/com/lambdaschool/usermodel/controllers/AuthorControllerTest.java new file mode 100644 index 0000000..f5d9230 --- /dev/null +++ b/oauth2/src/test/java/com/lambdaschool/usermodel/controllers/AuthorControllerTest.java @@ -0,0 +1,12 @@ +package com.lambdaschool.usermodel.controllers; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class AuthorControllerTest { + + @Test + public void listAllAuthors() { + } +} \ No newline at end of file From 6554d6cfdbabd2f304be9fc770896030e8fc9d8c Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 13:21:20 -0700 Subject: [PATCH 22/35] Create data.sql --- .../java/com/lambdaschool/usermodel/data.sql | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 oauth2/src/main/java/com/lambdaschool/usermodel/data.sql diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/data.sql b/oauth2/src/main/java/com/lambdaschool/usermodel/data.sql new file mode 100644 index 0000000..7c16f58 --- /dev/null +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/data.sql @@ -0,0 +1,27 @@ +INSERT INTO section (sectionid, name, created_by, created_date, last_modified_by, last_modified_date) VALUES (1, 'Fiction', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +INSERT INTO section (sectionid, name, created_by, created_date, last_modified_by, last_modified_date) VALUES (2, 'Technology', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +INSERT INTO section (sectionid, name, created_by, created_date, last_modified_by, last_modified_date) VALUES (3, 'Travel', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +INSERT INTO section (sectionid, name, created_by, created_date, last_modified_by, last_modified_date) VALUES (4, 'Business', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +INSERT INTO section (sectionid, name, created_by, created_date, last_modified_by, last_modified_date) VALUES (5, 'Religion', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); + +INSERT INTO author (authorid, fname, lname, created_by, created_date, last_modified_by, last_modified_date) VALUES (1, 'John', 'Mitchell', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +INSERT INTO author (authorid, fname, lname, created_by, created_date, last_modified_by, last_modified_date) VALUES (2, 'Dan', 'Brown', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +INSERT INTO author (authorid, fname, lname, created_by, created_date, last_modified_by, last_modified_date) VALUES (3, 'Jerry', 'Poe', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +INSERT INTO author (authorid, fname, lname, created_by, created_date, last_modified_by, last_modified_date) VALUES (4, 'Wells', 'Teague', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +INSERT INTO author (authorid, fname, lname, created_by, created_date, last_modified_by, last_modified_date) VALUES (5, 'George', 'Gallinger', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +INSERT INTO author (authorid, fname, lname, created_by, created_date, last_modified_by, last_modified_date) VALUES (6, 'Ian', 'Stewart', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); + +INSERT INTO book (bookid, title, ISBN, copy, sectionid, created_by, created_date, last_modified_by, last_modified_date) VALUES (1, 'Flatterland', '9780738206752', 2001, 1, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +INSERT INTO book (bookid, title, ISBN, copy, sectionid, created_by, created_date, last_modified_by, last_modified_date) VALUES (2, 'Digital Fortess', '9788489367012', 2007, 1, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +INSERT INTO book (bookid, title, ISBN, copy, sectionid, created_by, created_date, last_modified_by, last_modified_date) VALUES (3, 'The Da Vinci Code', '9780307474278', 2009, 1, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +INSERT INTO book (bookid, title, ISBN, copy, sectionid, created_by, created_date, last_modified_by, last_modified_date) VALUES (4, 'Essentials of Finance', '1314241651234', 0, 4, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +INSERT INTO book (bookid, title, ISBN, copy, sectionid, created_by, created_date, last_modified_by, last_modified_date) VALUES (5, 'Calling Texas Home', '1885171382134', 2000, 3, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); + +INSERT INTO wrote (bookid, authorid, created_by, created_date, last_modified_by, last_modified_date) VALUES (1, 6, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +INSERT INTO wrote (bookid, authorid, created_by, created_date, last_modified_by, last_modified_date) VALUES (2, 2, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +INSERT INTO wrote (bookid, authorid, created_by, created_date, last_modified_by, last_modified_date) VALUES (3, 2, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +INSERT INTO wrote (bookid, authorid, created_by, created_date, last_modified_by, last_modified_date) VALUES (4, 5, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +INSERT INTO wrote (bookid, authorid, created_by, created_date, last_modified_by, last_modified_date) VALUES (4, 3, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +INSERT INTO wrote (bookid, authorid, created_by, created_date, last_modified_by, last_modified_date) VALUES (5, 4, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); + +alter sequence hibernate_sequence restart with 25; \ No newline at end of file From bbab378a73768235601a91c1b668aa8a18df4712 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 13:33:37 -0700 Subject: [PATCH 23/35] Update application.properties actuator exposure --- oauth2/src/main/resources/application.properties | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/oauth2/src/main/resources/application.properties b/oauth2/src/main/resources/application.properties index 2dc89f9..6bda111 100644 --- a/oauth2/src/main/resources/application.properties +++ b/oauth2/src/main/resources/application.properties @@ -44,3 +44,8 @@ spring.jpa.show-sql=false spring.jpa.properties.hibernate.format_sql=false # Turns off Spring Boot automatic exception handling server.error.whitelabel.enabled=false + +management.endpoints.web.exposure.include=* +management.endpoint.health.show-details=always + +spring.data.web.pageable.one-indexed-parameters=true From cd4f1bb386724683dfc127d5dfad2e5ed6d070dc Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 13:44:35 -0700 Subject: [PATCH 24/35] Update pom.xml for actuator --- oauth2/pom.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/oauth2/pom.xml b/oauth2/pom.xml index d7b5961..9234ae6 100644 --- a/oauth2/pom.xml +++ b/oauth2/pom.xml @@ -64,6 +64,12 @@ test + + + org.springframework.boot + spring-boot-starter-actuator + + org.springframework.security.oauth From 711fe99898cb06f1313ad6a3847812be330bf330 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 14:13:24 -0700 Subject: [PATCH 25/35] createdBookController to get the list of books. Anyone has access to books Only admins have access to authors --- .../usermodel/controllers/BookController.java | 32 +++++++++++++++++++ .../usermodel/services/BookService.java | 1 + .../usermodel/services/BookServiceImpl.java | 3 +- 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 oauth2/src/main/java/com/lambdaschool/usermodel/controllers/BookController.java diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/BookController.java b/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/BookController.java new file mode 100644 index 0000000..375f4ed --- /dev/null +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/BookController.java @@ -0,0 +1,32 @@ +package com.lambdaschool.usermodel.controllers; + +import com.lambdaschool.usermodel.models.Book; +import com.lambdaschool.usermodel.services.BookService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping(value = "/books") +public class BookController { + + @Autowired + private BookService bookService; + + private static final Logger logger = LoggerFactory.getLogger(BookController.class); + + // http://localhost:2021/books/books + @GetMapping(value = "/books", produces = "application/json") + ResponseEntity getBooks(){ + logger.info("books/books Accessed"); + List books = bookService.findAll(); + return new ResponseEntity<>(books, HttpStatus.OK); + } +} \ No newline at end of file diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java index f4f61a0..4f01d2e 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java @@ -1,6 +1,7 @@ package com.lambdaschool.usermodel.services; import com.lambdaschool.usermodel.models.Book; +import org.springframework.stereotype.Service; import java.util.List; diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java index 9eeca3b..5b742d5 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java @@ -5,10 +5,11 @@ import com.lambdaschool.usermodel.repository.AuthorRepo; import com.lambdaschool.usermodel.repository.BookRepo; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; - +@Service(value = "bookService") public class BookServiceImpl implements BookService { @Autowired From 8dc53839e6b1abc9c526bc1084e58c60caedd4dd Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 14:15:27 -0700 Subject: [PATCH 26/35] Create BookControllerTest.java --- .../usermodel/controllers/BookControllerTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 oauth2/src/test/java/com/lambdaschool/usermodel/controllers/BookControllerTest.java diff --git a/oauth2/src/test/java/com/lambdaschool/usermodel/controllers/BookControllerTest.java b/oauth2/src/test/java/com/lambdaschool/usermodel/controllers/BookControllerTest.java new file mode 100644 index 0000000..5390531 --- /dev/null +++ b/oauth2/src/test/java/com/lambdaschool/usermodel/controllers/BookControllerTest.java @@ -0,0 +1,12 @@ +package com.lambdaschool.usermodel.controllers; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class BookControllerTest { + + @Test + public void getBooks() { + } +} \ No newline at end of file From bf01a2aeb6f546b61cfd3eb7c74e79b9e38677eb Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Fri, 15 Nov 2019 17:16:23 -0700 Subject: [PATCH 27/35] bookController --- .../usermodel/controllers/BookController.java | 27 ++++++++++++++++--- .../usermodel/services/BookService.java | 1 + .../usermodel/services/BookServiceImpl.java | 2 ++ .../controllers/BookControllerTest.java | 16 +++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/BookController.java b/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/BookController.java index 375f4ed..a8e0b44 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/BookController.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/BookController.java @@ -7,10 +7,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; +import javax.servlet.http.HttpServletRequest; import java.util.List; @RestController @@ -29,4 +28,26 @@ ResponseEntity getBooks(){ List books = bookService.findAll(); return new ResponseEntity<>(books, HttpStatus.OK); } + + // http://localhost:2021/books/books/1 + @PutMapping(value = "/books/{id}", consumes = {"application/json"}) + public ResponseEntity updateBookById(HttpServletRequest request, @RequestBody Book book, @PathVariable long bookid) { + logger.info(request.getMethod().toUpperCase() + " " + request.getRequestURI() + " accessed"); + bookService.update(bookid, book); + return new ResponseEntity<>(HttpStatus.OK); + } + + @PostMapping(value = "/books/{bookid}/authors/{authorid}", consumes = {"application/json"}) + public ResponseEntity postBookAuthorById(HttpServletRequest request, @PathVariable long bookid, @PathVariable Book authorid) { + logger.info(request.getMethod().toUpperCase() + " " + request.getRequestURI() + " accessed"); + bookService.update(bookid, authorid); + return new ResponseEntity(HttpStatus.CREATED); + } + + @DeleteMapping(value = "/books/{id}") + public ResponseEntity deleteBook(HttpServletRequest request, @PathVariable long id) { + logger.info(request.getMethod().toUpperCase() + " " + request.getRequestURI() + " accessed"); + bookService.delete(id); + return new ResponseEntity<>(HttpStatus.OK); + } } \ No newline at end of file diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java index 4f01d2e..ba99808 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java @@ -16,4 +16,5 @@ public interface BookService { Book save(Book book); Book update(long id, Book book); + } diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java index 5b742d5..39e4092 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java @@ -1,5 +1,6 @@ package com.lambdaschool.usermodel.services; +import com.lambdaschool.usermodel.exceptions.ResourceFoundException; import com.lambdaschool.usermodel.exceptions.ResourceNotFoundException; import com.lambdaschool.usermodel.models.Book; import com.lambdaschool.usermodel.repository.AuthorRepo; @@ -59,4 +60,5 @@ public Book update(long id, Book book) { return bookRepo.save(book); } + } diff --git a/oauth2/src/test/java/com/lambdaschool/usermodel/controllers/BookControllerTest.java b/oauth2/src/test/java/com/lambdaschool/usermodel/controllers/BookControllerTest.java index 5390531..7e49a6d 100644 --- a/oauth2/src/test/java/com/lambdaschool/usermodel/controllers/BookControllerTest.java +++ b/oauth2/src/test/java/com/lambdaschool/usermodel/controllers/BookControllerTest.java @@ -9,4 +9,20 @@ public class BookControllerTest { @Test public void getBooks() { } + + @Test + public void testGetBooks() { + } + + @Test + public void updateBookById() { + } + + @Test + public void postBookAuthorById() { + } + + @Test + public void deleteBook() { + } } \ No newline at end of file From f283bc2ba7f92441142cca6638b25d0331cd4178 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Sat, 16 Nov 2019 00:14:25 -0700 Subject: [PATCH 28/35] oh rebuilt project lloks like it just removed imports --- oauth2/pom.xml | 2 +- .../lambdaschool/usermodel/models/Wrote.java | 1 - .../usermodel/repository/AuthorRepo.java | 1 - .../usermodel/services/AuthorService.java | 1 - .../usermodel/services/AuthorServiceImpl.java | 10 +- .../usermodel/services/BookService.java | 1 - .../usermodel/services/BookServiceImpl.java | 1 - .../usermodel/services/SectionService.java | 1 - .../services/SectionServiceImpl.java | 3 - .../src/main/resources/application.properties | 4 +- .../com/lambdaschool/usermodel/SeedData.java | 137 +++++++----------- 11 files changed, 53 insertions(+), 109 deletions(-) diff --git a/oauth2/pom.xml b/oauth2/pom.xml index 9234ae6..2072138 100644 --- a/oauth2/pom.xml +++ b/oauth2/pom.xml @@ -114,7 +114,7 @@ - jrmmba-usermodel + jrmmba-usermodel23 diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java index 44020db..75e3d59 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java @@ -4,7 +4,6 @@ import javax.persistence.*; import java.io.Serializable; -import java.util.Objects; @Entity @Table(name = "wrote") diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/repository/AuthorRepo.java b/oauth2/src/main/java/com/lambdaschool/usermodel/repository/AuthorRepo.java index 857f3c8..a53dce4 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/repository/AuthorRepo.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/repository/AuthorRepo.java @@ -1,7 +1,6 @@ package com.lambdaschool.usermodel.repository; import com.lambdaschool.usermodel.models.Author; -import com.lambdaschool.usermodel.models.User; import org.springframework.data.repository.CrudRepository; public interface AuthorRepo extends CrudRepository { diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorService.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorService.java index 1d58cc5..40def41 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorService.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorService.java @@ -1,7 +1,6 @@ package com.lambdaschool.usermodel.services; import com.lambdaschool.usermodel.models.Author; -import org.springframework.data.domain.Pageable; import java.util.List; diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorServiceImpl.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorServiceImpl.java index 1cb8dcf..305aecd 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorServiceImpl.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/AuthorServiceImpl.java @@ -1,20 +1,12 @@ package com.lambdaschool.usermodel.services; -import com.lambdaschool.usermodel.exceptions.ResourceFoundException; import com.lambdaschool.usermodel.exceptions.ResourceNotFoundException; import com.lambdaschool.usermodel.logging.Loggable; -import com.lambdaschool.usermodel.models.*; +import com.lambdaschool.usermodel.models.Author; import com.lambdaschool.usermodel.repository.AuthorRepo; import com.lambdaschool.usermodel.repository.BookRepo; -import com.lambdaschool.usermodel.repository.RoleRepository; -import com.lambdaschool.usermodel.repository.UserRepository; -import com.lambdaschool.usermodel.view.UserNameCountEmails; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.Pageable; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java index ba99808..fb09d84 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookService.java @@ -1,7 +1,6 @@ package com.lambdaschool.usermodel.services; import com.lambdaschool.usermodel.models.Book; -import org.springframework.stereotype.Service; import java.util.List; diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java index 39e4092..bdc9055 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/BookServiceImpl.java @@ -1,6 +1,5 @@ package com.lambdaschool.usermodel.services; -import com.lambdaschool.usermodel.exceptions.ResourceFoundException; import com.lambdaschool.usermodel.exceptions.ResourceNotFoundException; import com.lambdaschool.usermodel.models.Book; import com.lambdaschool.usermodel.repository.AuthorRepo; diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionService.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionService.java index 06b3f09..48d264b 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionService.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionService.java @@ -1,6 +1,5 @@ package com.lambdaschool.usermodel.services; -import com.lambdaschool.usermodel.models.Role; import com.lambdaschool.usermodel.models.Section; import java.util.List; diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionServiceImpl.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionServiceImpl.java index b5fd92a..199fdb9 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionServiceImpl.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/SectionServiceImpl.java @@ -3,12 +3,9 @@ import com.lambdaschool.usermodel.exceptions.ResourceFoundException; import com.lambdaschool.usermodel.exceptions.ResourceNotFoundException; import com.lambdaschool.usermodel.logging.Loggable; -import com.lambdaschool.usermodel.models.Role; import com.lambdaschool.usermodel.models.Section; import com.lambdaschool.usermodel.repository.AuthorRepo; -import com.lambdaschool.usermodel.repository.RoleRepository; import com.lambdaschool.usermodel.repository.SectionRepo; -import com.lambdaschool.usermodel.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; diff --git a/oauth2/src/main/resources/application.properties b/oauth2/src/main/resources/application.properties index 6bda111..784a0c3 100644 --- a/oauth2/src/main/resources/application.properties +++ b/oauth2/src/main/resources/application.properties @@ -2,8 +2,8 @@ # for a list of application.properties # Environment Variables Needed for PostgreSQL database # Which db to run -local.run.db=H2 -# local.run.db=POSTGRESQL +# local.run.db=H2 +local.run.db=POSTGRESQL # MYDBHOST # MYDBNAME # MYDBUSER diff --git a/oauth2/src/test/java/com/lambdaschool/usermodel/SeedData.java b/oauth2/src/test/java/com/lambdaschool/usermodel/SeedData.java index 3dc6e13..a77cca4 100755 --- a/oauth2/src/test/java/com/lambdaschool/usermodel/SeedData.java +++ b/oauth2/src/test/java/com/lambdaschool/usermodel/SeedData.java @@ -8,111 +8,72 @@ import com.lambdaschool.usermodel.services.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; +import java.util.List; -@Transactional @Component -public class SeedData implements CommandLineRunner -{ - @Autowired - RoleService roleService; +@Transactional +public class SeedData implements CommandLineRunner { @Autowired UserService userService; - @Override - public void run(String[] args) throws Exception - { - Role r1 = new Role("admin"); - Role r2 = new Role("user"); - Role r3 = new Role("data"); + public void run(String... args) throws Exception { + + Pageable pageable = new Pageable() { + @Override + public int getPageNumber() { + return 0; + } + + @Override + public int getPageSize() { + return 10; + } + + @Override + public long getOffset() { + return 0; + } - roleService.save(r1); - roleService.save(r2); - roleService.save(r3); + @Override + public Sort getSort() { + return null; + } - // admin, data, user - ArrayList admins = new ArrayList<>(); - admins.add(new UserRoles(new User(), - r1)); - admins.add(new UserRoles(new User(), - r2)); - admins.add(new UserRoles(new User(), - r3)); - User u1 = new User("admin", - "ILuvM4th!", - "admin@lambdaschool.local", - admins); - u1.getUseremails() - .add(new Useremail(u1, - "admin@email.local")); - u1.getUseremails() - .add(new Useremail(u1, - "admin@mymail.local")); - u1 = userService.save(u1); + @Override + public Pageable next() { + return null; + } - // data, user - ArrayList datas = new ArrayList<>(); - datas.add(new UserRoles(new User(), - r3)); - datas.add(new UserRoles(new User(), - r2)); - User u2 = new User("cinnamon", - "1234567", - "cinnamon@lambdaschool.local", - datas); - u2.getUseremails() - .add(new Useremail(u2, - "cinnamon@mymail.local")); - u2.getUseremails() - .add(new Useremail(u2, - "hops@mymail.local")); - u2.getUseremails() - .add(new Useremail(u2, - "bunny@email.local")); - u2 = userService.save(u2); + @Override + public Pageable previousOrFirst() { + return null; + } - // user - ArrayList users = new ArrayList<>(); - users.add(new UserRoles(new User(), - r1)); - User u3 = new User("testbarn", - "ILuvM4th!", - "testbarn@school.lambda", - users); - u3.getUseremails() - .add(new Useremail(u3, - "barnbarn@email.local")); - u3 = userService.save(u3); + @Override + public Pageable first() { + return null; + } - users = new ArrayList<>(); - users.add(new UserRoles(new User(), - r2)); - User u4 = new User("testcat", - "password", - "testcat@school.lambda", - users); - u4 = userService.save(u4); + @Override + public boolean hasPrevious() { + return false; + } + }; - users = new ArrayList<>(); - users.add(new UserRoles(new User(), - r2)); - User u5 = new User("testdog", - "password", - "testdog@school.lambda", - users); - u5 = userService.save(u5); + List userList = new ArrayList<>(); + userService.findAll(pageable).iterator().forEachRemaining(userList::add); - System.out.println("\n*** Seed Data ***"); - System.out.println(u1); - System.out.println(u2); - System.out.println(u3); - System.out.println(u4); - System.out.println(u5); - System.out.println("*** Seed Data ***\n"); + for (User u : userList) { + u.setUserroles(new ArrayList<>()); + userService.update(u, u.getUserid(), true); + } } } \ No newline at end of file From 09403002b8d7bcd29f733e1f3ba7920c0314b667 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Sun, 17 Nov 2019 20:14:39 -0700 Subject: [PATCH 29/35] got past the password issue --- oauth2/pom.xml | 12 +++++------ .../com/lambdaschool/usermodel/SeedData.java | 2 +- .../config/AuthorizationServerConfig.java | 4 +++- .../usermodel/config/DataSourceConfig.java | 15 ++++++------- .../config/ResourceServerConfig.java | 2 ++ .../usermodel/config/SecurityConfig.java | 4 ++++ .../controllers/LogoutController.java | 2 +- .../lambdaschool/usermodel/models/Author.java | 4 +++- .../lambdaschool/usermodel/models/Book.java | 5 ++++- .../lambdaschool/usermodel/models/User.java | 6 ++++-- .../services/SecurityUserServiceImpl.java | 2 ++ .../src/main/resources/application.properties | 6 +++--- .../src/test/resources/application.properties | 21 ++++++++++--------- 13 files changed, 51 insertions(+), 34 deletions(-) diff --git a/oauth2/pom.xml b/oauth2/pom.xml index 2072138..8fe4cda 100644 --- a/oauth2/pom.xml +++ b/oauth2/pom.xml @@ -64,12 +64,6 @@ test - - - org.springframework.boot - spring-boot-starter-actuator - - org.springframework.security.oauth @@ -111,10 +105,14 @@ 4.1.1 test + + org.springframework.boot + spring-boot-starter-actuator + - jrmmba-usermodel23 + ay-usermodel diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/SeedData.java b/oauth2/src/main/java/com/lambdaschool/usermodel/SeedData.java index 078798a..560c2b5 100755 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/SeedData.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/SeedData.java @@ -85,7 +85,7 @@ public void run(String[] args) throws Exception ArrayList users = new ArrayList<>(); users.add(new UserRoles(new User(), r2)); - User u3 = new User("barnbarn", + User u3 = new User("postgres", "ILuvM4th!", "barnbarn@lambdaschool.local", users); diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/config/AuthorizationServerConfig.java b/oauth2/src/main/java/com/lambdaschool/usermodel/config/AuthorizationServerConfig.java index 67db946..3b9cd82 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/config/AuthorizationServerConfig.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/config/AuthorizationServerConfig.java @@ -10,6 +10,8 @@ import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer; import org.springframework.security.oauth2.provider.token.TokenStore; +// TODO AUTH 1 + @Configuration @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter @@ -17,7 +19,7 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap static final String CLIENT_ID = System.getenv("OAUTHCLIENTID"); static final String CLIENT_SECRET = System.getenv("OAUTHCLIENTSECRET"); - static final String GRANT_TYPE_PASSWORD = "password"; + static final String GRANT_TYPE_PASSWORD = "Acura35RL!"; static final String AUTHORIZATION_CODE = "authorization_code"; static final String IMPLICIT = "implicit"; static final String SCOPE_READ = "read"; diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/config/DataSourceConfig.java b/oauth2/src/main/java/com/lambdaschool/usermodel/config/DataSourceConfig.java index c6cd34a..f351266 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/config/DataSourceConfig.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/config/DataSourceConfig.java @@ -1,5 +1,4 @@ package com.lambdaschool.usermodel.config; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -15,6 +14,8 @@ import javax.sql.DataSource; + +//TODO 8 db switch @Configuration public class DataSourceConfig { @@ -56,7 +57,7 @@ public DataSource dataSource() { logger.info("Manually shutting down system"); int exitCode = SpringApplication.exit(appContext, - (ExitCodeGenerator) () -> 1); + () -> 1); System.exit(exitCode); } @@ -75,11 +76,11 @@ public DataSource dataSource() logger.info("Database URL is " + myUrlString); return DataSourceBuilder.create() - .username(myDBUser) - .password(myDBPassword) - .url(myUrlString) - .driverClassName(myDriverClass) - .build(); + .username(myDBUser) + .password(myDBPassword) + .url(myUrlString) + .driverClassName(myDriverClass) + .build(); } @Bean(name = "jdbcCustom") diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/config/ResourceServerConfig.java b/oauth2/src/main/java/com/lambdaschool/usermodel/config/ResourceServerConfig.java index c86f3c2..3772881 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/config/ResourceServerConfig.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/config/ResourceServerConfig.java @@ -7,6 +7,8 @@ import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer; import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler; +//TODO AUTH 6 + @Configuration @EnableResourceServer public class ResourceServerConfig extends ResourceServerConfigurerAdapter diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/config/SecurityConfig.java b/oauth2/src/main/java/com/lambdaschool/usermodel/config/SecurityConfig.java index 99e0ec5..3fad2e6 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/config/SecurityConfig.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/config/SecurityConfig.java @@ -16,11 +16,15 @@ import javax.annotation.Resource; + +//TODO AUTH 2 + @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class SecurityConfig extends WebSecurityConfigurerAdapter { + @Resource(name = "securityUserService") private UserDetailsService userDetailsService; diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/LogoutController.java b/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/LogoutController.java index f2849ae..3f08014 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/LogoutController.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/LogoutController.java @@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.ResponseStatus; import javax.servlet.http.HttpServletRequest; - +//TODO AUTH 7 this is to logout @Loggable @Controller @Api(tags = {"LogoutEndpoint"}) diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java index 252b602..6aa0ea6 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java @@ -28,9 +28,11 @@ public class Author extends Auditable{ public Author() {} - public Author(String fname, String lname) { + public Author(String fname, String lname, List books, List writer) { this.fname = fname; this.lname = lname; + this.books = books; + this.writer = writer; } public long getBookid() { diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java index 5e9b6d1..f8e377e 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java @@ -37,10 +37,13 @@ public class Book extends Auditable { public Book(){} - public Book(String title, String ISBN, int copy) { + public Book(String title, String ISBN, int copy, List authors, Section section, List writers) { this.title = title; this.ISBN = ISBN; this.copy = copy; + this.authors = authors; + this.section = section; + this.writers = writers; } public List getAuthors() { diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/User.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/User.java index d79db41..d32d2e9 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/User.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/User.java @@ -130,13 +130,13 @@ public String getPassword() { return password; } - +//TODO AUTH 5 ENCRYPT THE PASSWORD public void setPassword(String password) { BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); this.password = passwordEncoder.encode(password); } - +//TODO AUTH 6 NON ENCODED public void setPasswordNotEncrypt(String password) { this.password = password; @@ -162,6 +162,8 @@ public void setUseremails(List useremails) this.useremails = useremails; } + //TODO AUTH 4 SimpleGrantAuthority + @JsonIgnore public List getAuthority() { diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/services/SecurityUserServiceImpl.java b/oauth2/src/main/java/com/lambdaschool/usermodel/services/SecurityUserServiceImpl.java index 7dae64d..e5cad28 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/services/SecurityUserServiceImpl.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/services/SecurityUserServiceImpl.java @@ -10,6 +10,8 @@ import javax.transaction.Transactional; +// TODO AUTH 3 (helps logining in) + @Service(value = "securityUserService") public class SecurityUserServiceImpl implements UserDetailsService { diff --git a/oauth2/src/main/resources/application.properties b/oauth2/src/main/resources/application.properties index 784a0c3..60532b0 100644 --- a/oauth2/src/main/resources/application.properties +++ b/oauth2/src/main/resources/application.properties @@ -19,6 +19,7 @@ local.run.db=POSTGRESQL # posgress://username :password @url host :5432/dbname server.port=${PORT:2021} # Begin h2 configuration +# Begin h2 configuration spring.h2.console.enabled=true spring.h2.console.path=/h2-console spring.h2.console.settings.web-allow-others=true @@ -40,10 +41,9 @@ spring.jackson.serialization.fail-on-empty-beans=false # so you will have the same EntityManager until the web request is finished. spring.jpa.open-in-view=true # Shows the generated and custom SQL but does hurt performance and the logs get full fast -spring.jpa.show-sql=false +spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=false -# Turns off Spring Boot automatic exception handling -server.error.whitelabel.enabled=false +# Turns off Spring Boot automatic excepti management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always diff --git a/oauth2/src/test/resources/application.properties b/oauth2/src/test/resources/application.properties index 6953168..3fef8ec 100644 --- a/oauth2/src/test/resources/application.properties +++ b/oauth2/src/test/resources/application.properties @@ -2,8 +2,8 @@ # for a list of application.properties # Environment Variables Needed for PostgreSQL database # Which db to run -local.run.db=H2 -# local.run.db=POSTGRESQL +# local.run.db=H2 +local.run.db=POSTGRESQL # MYDBHOST # MYDBNAME # MYDBUSER @@ -19,17 +19,20 @@ local.run.db=H2 # posgress://username :password @url host :5432/dbname server.port=${PORT:2019} # Begin h2 configuration +# Begin h2 configuration spring.h2.console.enabled=true spring.h2.console.path=/h2-console spring.h2.console.settings.web-allow-others=true # End h2 configuration +# Should we use createBlob from java.sql.connection. PostgreSQL requires it to be true. +spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true # What do with the schema # drop n create table again, good for testing -# spring.jpa.hibernate.ddl-auto=create -# spring.datasource.initialization-mode=always +spring.jpa.hibernate.ddl-auto=create +spring.datasource.initialization-mode=always # Good for production! -spring.jpa.hibernate.ddl-auto=update -spring.datasource.initialization-mode=never +# spring.jpa.hibernate.ddl-auto=update +# spring.datasource.initialization-mode=never # Feature that determines what happens when no accessors are found for a type # (and there are no annotations to indicate it is meant to be serialized). spring.jackson.serialization.fail-on-empty-beans=false @@ -40,7 +43,5 @@ spring.jpa.open-in-view=true # Shows the generated and custom SQL but does hurt performance and the logs get full fast spring.jpa.show-sql=false spring.jpa.properties.hibernate.format_sql=false -# Turns off Spring Boot automatic exception handling -server.error.whitelabel.enabled=false -# Should we use createBlob from java.sql.connection. PostgreSQL requires it to be true. -spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true +# Turns off Spring Boot automatic excepti + From c8f05f7009d02e6b65c52f8e08c633c7ae62b038 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Sun, 17 Nov 2019 21:43:58 -0700 Subject: [PATCH 30/35] fixed the looping issue --- .../lambdaschool/usermodel/models/Author.java | 25 +++++++------------ .../lambdaschool/usermodel/models/Book.java | 25 ++++++++----------- .../lambdaschool/usermodel/models/Wrote.java | 19 ++++++++++++-- .../src/main/resources/application.properties | 2 +- .../usermodel => resources}/data.sql | 4 ++- 5 files changed, 41 insertions(+), 34 deletions(-) rename oauth2/src/main/{java/com/lambdaschool/usermodel => resources}/data.sql (98%) diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java index 6aa0ea6..ad895c4 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Author.java @@ -9,18 +9,18 @@ @Loggable @Entity -@Table(name = "authors") +@Table(name = "author") public class Author extends Auditable{ @Id @GeneratedValue(strategy = GenerationType.AUTO) - private long bookid; + private long authorid; private String fname; private String lname; - @ManyToMany(mappedBy = "authors", cascade = CascadeType.ALL) - @JsonIgnoreProperties(value = "authors") - private List books = new ArrayList<>(); + // @ManyToMany(mappedBy = "authors", cascade = CascadeType.ALL) + // @JsonIgnoreProperties(value = "authors") + // private List books = new ArrayList<>(); @OneToMany(mappedBy = "author", cascade = CascadeType.ALL, orphanRemoval = true) @JsonIgnoreProperties("author") @@ -28,19 +28,17 @@ public class Author extends Auditable{ public Author() {} - public Author(String fname, String lname, List books, List writer) { + public Author(String fname, String lname) { this.fname = fname; this.lname = lname; - this.books = books; - this.writer = writer; } public long getBookid() { - return bookid; + return authorid; } public void setBookid(long bookid) { - this.bookid = bookid; + this.authorid = bookid; } public String getFname() { @@ -63,13 +61,8 @@ public void setWriter(List writer) { this.writer = writer; } - public List getBooks() { - return books; - } - public void setBooks(List books) { - this.books = books; - } + public void setLname(String lname) { this.lname = lname; diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java index f8e377e..6840761 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Book.java @@ -9,7 +9,7 @@ @Loggable @Entity -@Table(name = "books") +@Table(name = "book") public class Book extends Auditable { @Id @@ -21,9 +21,9 @@ public class Book extends Auditable { @Column(nullable = true) private int copy; - @ManyToMany - @JsonIgnoreProperties("books") - private List authors = new ArrayList<>(); + // @ManyToMany + // @JsonIgnoreProperties("books") + // private List authors = new ArrayList<>(); @ManyToOne @JoinColumn(name = "sectionid") @@ -37,22 +37,19 @@ public class Book extends Auditable { public Book(){} - public Book(String title, String ISBN, int copy, List authors, Section section, List writers) { + public Book(String title, String ISBN, int copy,Section section) { this.title = title; this.ISBN = ISBN; this.copy = copy; - this.authors = authors; this.section = section; - this.writers = writers; } +// public List getAuthors() { +// return authors; +// } - public List getAuthors() { - return authors; - } - - public void setAuthors(List authors) { - this.authors = authors; - } +// public void setAuthors(List authors) { +// this.authors = authors; +// } public Section getSection() { return section; diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java index 75e3d59..d76bd40 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/Wrote.java @@ -4,6 +4,7 @@ import javax.persistence.*; import java.io.Serializable; +import java.util.Objects; @Entity @Table(name = "wrote") @@ -11,13 +12,13 @@ public class Wrote extends Auditable implements Serializable { @Id @ManyToOne @JoinColumn(name = "bookid") - @JsonIgnoreProperties("wrote") + @JsonIgnoreProperties("writers") private Book book; @Id @ManyToOne @JoinColumn(name = "authorid") - @JsonIgnoreProperties("wrote") + @JsonIgnoreProperties("writers") private Author author; public Wrote() { @@ -44,4 +45,18 @@ public void setAuthor(Author author) { this.author = author; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Wrote)) return false; + Wrote wrote = (Wrote) o; + return Objects.equals(getBook(), wrote.getBook()) && + Objects.equals(getAuthor(), wrote.getAuthor()); + } + + @Override + public int hashCode() { + return Objects.hash(getBook(), getAuthor()); + } } diff --git a/oauth2/src/main/resources/application.properties b/oauth2/src/main/resources/application.properties index 60532b0..66ed618 100644 --- a/oauth2/src/main/resources/application.properties +++ b/oauth2/src/main/resources/application.properties @@ -41,7 +41,7 @@ spring.jackson.serialization.fail-on-empty-beans=false # so you will have the same EntityManager until the web request is finished. spring.jpa.open-in-view=true # Shows the generated and custom SQL but does hurt performance and the logs get full fast -spring.jpa.show-sql=true +spring.jpa.show-sql=false spring.jpa.properties.hibernate.format_sql=false # Turns off Spring Boot automatic excepti diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/data.sql b/oauth2/src/main/resources/data.sql similarity index 98% rename from oauth2/src/main/java/com/lambdaschool/usermodel/data.sql rename to oauth2/src/main/resources/data.sql index 7c16f58..5bba40f 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/data.sql +++ b/oauth2/src/main/resources/data.sql @@ -1,3 +1,4 @@ + INSERT INTO section (sectionid, name, created_by, created_date, last_modified_by, last_modified_date) VALUES (1, 'Fiction', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); INSERT INTO section (sectionid, name, created_by, created_date, last_modified_by, last_modified_date) VALUES (2, 'Technology', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); INSERT INTO section (sectionid, name, created_by, created_date, last_modified_by, last_modified_date) VALUES (3, 'Travel', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); @@ -24,4 +25,5 @@ INSERT INTO wrote (bookid, authorid, created_by, created_date, last_modified_by, INSERT INTO wrote (bookid, authorid, created_by, created_date, last_modified_by, last_modified_date) VALUES (4, 3, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); INSERT INTO wrote (bookid, authorid, created_by, created_date, last_modified_by, last_modified_date) VALUES (5, 4, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); -alter sequence hibernate_sequence restart with 25; \ No newline at end of file +alter sequence hibernate_sequence restart with 25; + From b35a754ca8f8fc65d83c79125badc10e68eebf45 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Sun, 17 Nov 2019 21:57:50 -0700 Subject: [PATCH 31/35] Update BookController.java fixed updating a book --- .../lambdaschool/usermodel/controllers/BookController.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/BookController.java b/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/BookController.java index a8e0b44..c460268 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/BookController.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/BookController.java @@ -13,7 +13,7 @@ import java.util.List; @RestController -@RequestMapping(value = "/books") +@RequestMapping(value = "/book") public class BookController { @Autowired @@ -31,9 +31,9 @@ ResponseEntity getBooks(){ // http://localhost:2021/books/books/1 @PutMapping(value = "/books/{id}", consumes = {"application/json"}) - public ResponseEntity updateBookById(HttpServletRequest request, @RequestBody Book book, @PathVariable long bookid) { + public ResponseEntity updateBookById(HttpServletRequest request, @RequestBody Book book, @PathVariable long id) { logger.info(request.getMethod().toUpperCase() + " " + request.getRequestURI() + " accessed"); - bookService.update(bookid, book); + bookService.update(id, book); return new ResponseEntity<>(HttpStatus.OK); } From c7e684b6b5575a8573b7c1c14851b27374286842 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Sun, 17 Nov 2019 22:46:52 -0700 Subject: [PATCH 32/35] deploy LOL --- oauth2/pom.xml | 4 ++-- oauth2/src/main/resources/application.properties | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/oauth2/pom.xml b/oauth2/pom.xml index 8fe4cda..a8e6386 100644 --- a/oauth2/pom.xml +++ b/oauth2/pom.xml @@ -112,7 +112,7 @@ - ay-usermodel + als-usermodel @@ -131,7 +131,7 @@ false - ${project.build.directory}/${project.build.finalName}.jar + ${project.build.directory}\${project.build.finalName}.jar ${java.version} diff --git a/oauth2/src/main/resources/application.properties b/oauth2/src/main/resources/application.properties index 66ed618..9592c16 100644 --- a/oauth2/src/main/resources/application.properties +++ b/oauth2/src/main/resources/application.properties @@ -15,8 +15,8 @@ local.run.db=POSTGRESQL # heroku config -a jrmmba-oauthmin # postgres://rrwzjxlkniayov:83e8dc9dc5a3c3a30e40dde8fb62941da11030b3953709f5c8f808690e776c71@ec2-54-243-241-62.compute-1.amazonaws.com:5432/d7bl8dlv2l83jj # postgres://bzhxqmxdgwigkw:611e5353ffd763b09aed8fad07d243e9bbf44e13f8f542330e3b862a1df47c92@ec2-54-221-243-211.compute-1.amazonaws.com:5432/d1blvk8nmshe0v -# postgres://cdtpdlmrduoyqv:4958b9a3ba0533fa4fae763ac284bea71ea29f6955b9e36896740bed23289175@ec2-23-21-94-99.compute-1.amazonaws.com:5432/d37hecggdrqi5s -# posgress://username :password @url host :5432/dbname +# postgres://bgqdsfkwiafiqv:237e3deefb862a3a27a60419c9feb82015e7e4bd8d256f4b4bfd21a18a01a97e@ec2-107-21-98-89.compute-1.amazonaws.com:5432/d4mg9mi2871qnl\ +# posgress://username :password @url host :5432/dbname server.port=${PORT:2021} # Begin h2 configuration # Begin h2 configuration From 6a56d4485dfcf963ab3e9fd214af58c8088a750e Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Mon, 18 Nov 2019 09:58:54 -0700 Subject: [PATCH 33/35] got it!!!!!!!!!!! --- oauth2/pom.xml | 2 +- .../com/lambdaschool/usermodel/SeedData.java | 101 +++--- .../config/AuthorizationServerConfig.java | 2 +- .../config/H2ServerConfiguration.java | 5 +- .../src/main/resources/application.properties | 2 +- oauth2/src/main/resources/logback-spring.xml | 14 +- .../com/lambdaschool/usermodel/SeedData.java | 79 ---- .../usermodel/UserModelApplication.java | 28 -- .../services/UserServiceImplUnitTest.java | 336 ------------------ .../src/test/resources/application.properties | 4 +- 10 files changed, 71 insertions(+), 502 deletions(-) delete mode 100755 oauth2/src/test/java/com/lambdaschool/usermodel/SeedData.java delete mode 100644 oauth2/src/test/java/com/lambdaschool/usermodel/UserModelApplication.java delete mode 100644 oauth2/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplUnitTest.java diff --git a/oauth2/pom.xml b/oauth2/pom.xml index a8e6386..feba337 100644 --- a/oauth2/pom.xml +++ b/oauth2/pom.xml @@ -44,7 +44,7 @@ com.h2database h2 - + diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/SeedData.java b/oauth2/src/main/java/com/lambdaschool/usermodel/SeedData.java index 560c2b5..900e2b4 100755 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/SeedData.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/SeedData.java @@ -11,9 +11,20 @@ import com.lambdaschool.usermodel.services.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; -import javax.transaction.Transactional; import java.util.ArrayList; import java.util.Locale; @@ -42,74 +53,74 @@ public void run(String[] args) throws Exception // admin, data, user ArrayList admins = new ArrayList<>(); admins.add(new UserRoles(new User(), - r1)); + r1)); admins.add(new UserRoles(new User(), - r2)); + r2)); admins.add(new UserRoles(new User(), - r3)); + r3)); User u1 = new User("admin", - "password", - "admin@lambdaschool.local", - admins); + "password", + "admin@lambdaschool.local", + admins); u1.getUseremails() - .add(new Useremail(u1, - "admin@email.local")); + .add(new Useremail(u1, + "admin@email.local")); u1.getUseremails() - .add(new Useremail(u1, - "admin@mymail.local")); + .add(new Useremail(u1, + "admin@mymail.local")); userService.save(u1); // data, user ArrayList datas = new ArrayList<>(); datas.add(new UserRoles(new User(), - r3)); + r3)); datas.add(new UserRoles(new User(), - r2)); + r2)); User u2 = new User("cinnamon", - "1234567", - "cinnamon@lambdaschool.local", - datas); + "1234567", + "cinnamon@lambdaschool.local", + datas); u2.getUseremails() - .add(new Useremail(u2, - "cinnamon@mymail.local")); + .add(new Useremail(u2, + "cinnamon@mymail.local")); u2.getUseremails() - .add(new Useremail(u2, - "hops@mymail.local")); + .add(new Useremail(u2, + "hops@mymail.local")); u2.getUseremails() - .add(new Useremail(u2, - "bunny@email.local")); + .add(new Useremail(u2, + "bunny@email.local")); userService.save(u2); // user ArrayList users = new ArrayList<>(); users.add(new UserRoles(new User(), - r2)); - User u3 = new User("postgres", - "ILuvM4th!", - "barnbarn@lambdaschool.local", - users); + r2)); + User u3 = new User("barnbarn", + "ILuvM4th!", + "barnbarn@lambdaschool.local", + users); u3.getUseremails() - .add(new Useremail(u3, - "barnbarn@email.local")); + .add(new Useremail(u3, + "barnbarn@email.local")); userService.save(u3); users = new ArrayList<>(); users.add(new UserRoles(new User(), - r2)); + r2)); User u4 = new User("puttat", - "password", - "puttat@school.lambda", - users); + "password", + "puttat@school.lambda", + users); userService.save(u4); users = new ArrayList<>(); users.add(new UserRoles(new User(), - r2)); + r2)); User u5 = new User("misskitty", - "password", - "misskitty@school.lambda", - users); + "password", + "misskitty@school.lambda", + users); userService.save(u5); // using JavaFaker create a bunch of regular users @@ -117,7 +128,7 @@ public void run(String[] args) throws Exception // https://www.baeldung.com/regular-expressions-java FakeValuesService fakeValuesService = new FakeValuesService(new Locale("en-US"), - new RandomService()); + new RandomService()); Faker nameFaker = new Faker(new Locale("en-US")); for (int i = 0; i < 100; i++) @@ -127,16 +138,16 @@ public void run(String[] args) throws Exception users = new ArrayList<>(); users.add(new UserRoles(new User(), - r2)); + r2)); fakeUser = new User(nameFaker.name() - .username(), - "password", - nameFaker.internet() - .emailAddress(), - users); + .username(), + "password", + nameFaker.internet() + .emailAddress(), + users); fakeUser.getUseremails() .add(new Useremail(fakeUser, - fakeValuesService.bothify("????##@gmail.com"))); + fakeValuesService.bothify("????##@gmail.com"))); userService.save(fakeUser); } } diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/config/AuthorizationServerConfig.java b/oauth2/src/main/java/com/lambdaschool/usermodel/config/AuthorizationServerConfig.java index 3b9cd82..c878ed1 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/config/AuthorizationServerConfig.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/config/AuthorizationServerConfig.java @@ -19,7 +19,7 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap static final String CLIENT_ID = System.getenv("OAUTHCLIENTID"); static final String CLIENT_SECRET = System.getenv("OAUTHCLIENTSECRET"); - static final String GRANT_TYPE_PASSWORD = "Acura35RL!"; + static final String GRANT_TYPE_PASSWORD = "password"; static final String AUTHORIZATION_CODE = "authorization_code"; static final String IMPLICIT = "implicit"; static final String SCOPE_READ = "read"; diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/config/H2ServerConfiguration.java b/oauth2/src/main/java/com/lambdaschool/usermodel/config/H2ServerConfiguration.java index 6734947..69f9c9f 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/config/H2ServerConfiguration.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/config/H2ServerConfiguration.java @@ -1,5 +1,6 @@ package com.lambdaschool.usermodel.config; + import org.h2.tools.Server; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; @@ -15,11 +16,11 @@ public class H2ServerConfiguration { // TCP port for remote connections, default 9092 - @Value("${h2.tcp.port:9091}") + @Value("${h2.tcp.port:9092}") private String h2TcpPort; // Web port, default 8082 - @Value("${h2.web.port:8081}") + @Value("${h2.web.port:8082}") private String h2WebPort; /** diff --git a/oauth2/src/main/resources/application.properties b/oauth2/src/main/resources/application.properties index 9592c16..6b8a1ac 100644 --- a/oauth2/src/main/resources/application.properties +++ b/oauth2/src/main/resources/application.properties @@ -2,7 +2,7 @@ # for a list of application.properties # Environment Variables Needed for PostgreSQL database # Which db to run -# local.run.db=H2 +#local.run.db=H2 local.run.db=POSTGRESQL # MYDBHOST # MYDBNAME diff --git a/oauth2/src/main/resources/logback-spring.xml b/oauth2/src/main/resources/logback-spring.xml index df282be..e775e52 100644 --- a/oauth2/src/main/resources/logback-spring.xml +++ b/oauth2/src/main/resources/logback-spring.xml @@ -48,35 +48,35 @@ - + - + - + - + - + - + - + diff --git a/oauth2/src/test/java/com/lambdaschool/usermodel/SeedData.java b/oauth2/src/test/java/com/lambdaschool/usermodel/SeedData.java deleted file mode 100755 index a77cca4..0000000 --- a/oauth2/src/test/java/com/lambdaschool/usermodel/SeedData.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.lambdaschool.usermodel; - -import com.lambdaschool.usermodel.models.Role; -import com.lambdaschool.usermodel.models.User; -import com.lambdaschool.usermodel.models.UserRoles; -import com.lambdaschool.usermodel.models.Useremail; -import com.lambdaschool.usermodel.services.RoleService; -import com.lambdaschool.usermodel.services.UserService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.CommandLineRunner; -import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Sort; -import org.springframework.stereotype.Component; -import org.springframework.transaction.annotation.Transactional; - -import java.util.ArrayList; -import java.util.List; - -@Component -@Transactional -public class SeedData implements CommandLineRunner { - - @Autowired - UserService userService; - - @Override - public void run(String... args) throws Exception { - - Pageable pageable = new Pageable() { - @Override - public int getPageNumber() { - return 0; - } - - @Override - public int getPageSize() { - return 10; - } - - @Override - public long getOffset() { - return 0; - } - - @Override - public Sort getSort() { - return null; - } - - @Override - public Pageable next() { - return null; - } - - @Override - public Pageable previousOrFirst() { - return null; - } - - @Override - public Pageable first() { - return null; - } - - @Override - public boolean hasPrevious() { - return false; - } - }; - - List userList = new ArrayList<>(); - userService.findAll(pageable).iterator().forEachRemaining(userList::add); - - for (User u : userList) { - u.setUserroles(new ArrayList<>()); - userService.update(u, u.getUserid(), true); - } - } -} \ No newline at end of file diff --git a/oauth2/src/test/java/com/lambdaschool/usermodel/UserModelApplication.java b/oauth2/src/test/java/com/lambdaschool/usermodel/UserModelApplication.java deleted file mode 100644 index c10ea4d..0000000 --- a/oauth2/src/test/java/com/lambdaschool/usermodel/UserModelApplication.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.lambdaschool.usermodel; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.ApplicationContext; -import org.springframework.web.servlet.DispatcherServlet; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; - -@EnableWebMvc -// @EnableJpaAuditing -@SpringBootApplication -public class UserModelApplication -{ - private static final Logger logger = LoggerFactory.getLogger(com.lambdaschool.usermodel.UserModelApplication.class); - - - public static void main(String[] args) - { - ApplicationContext ctx = SpringApplication.run(com.lambdaschool.usermodel.UserModelApplication.class, - args); - - DispatcherServlet dispatcherServlet = (DispatcherServlet) ctx.getBean("dispatcherServlet"); - dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); - } - -} diff --git a/oauth2/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplUnitTest.java b/oauth2/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplUnitTest.java deleted file mode 100644 index 77ac21c..0000000 --- a/oauth2/src/test/java/com/lambdaschool/usermodel/services/UserServiceImplUnitTest.java +++ /dev/null @@ -1,336 +0,0 @@ -package com.lambdaschool.usermodel.services; - -import com.lambdaschool.usermodel.UserModelApplication; -import com.lambdaschool.usermodel.exceptions.ResourceFoundException; -import com.lambdaschool.usermodel.exceptions.ResourceNotFoundException; -import com.lambdaschool.usermodel.models.Role; -import com.lambdaschool.usermodel.models.User; -import com.lambdaschool.usermodel.models.UserRoles; -import com.lambdaschool.usermodel.models.Useremail; -import org.junit.After; -import org.junit.Before; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.MethodSorters; -import org.mockito.MockitoAnnotations; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.data.domain.Pageable; -import org.springframework.security.test.context.support.WithUserDetails; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.transaction.annotation.Transactional; - -import java.util.ArrayList; - -import static junit.framework.TestCase.assertEquals; - -/** - * I am testing UserServiceImpl so want 100% in UserServiceImpl - */ - -@RunWith(SpringRunner.class) -@SpringBootTest(classes = UserModelApplication.class) -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class UserServiceImplUnitTest -{ - @Autowired - private UserService userService; - - @Before - public void setUp() throws Exception - { - MockitoAnnotations.initMocks(this); - } - - @After - public void tearDown() throws Exception - { - } - - @Test - public void B_findUserById() - { - assertEquals("admin", - userService.findUserById(4) - .getUsername()); - } - - @Test(expected = ResourceNotFoundException.class) - public void BA_findUserByIdNotFound() - { - assertEquals("admin", - userService.findUserById(10) - .getUsername()); - } - - @Test - public void C_findAll() - { - assertEquals(5, - userService.findAll(Pageable.unpaged()) - .size()); - } - - @Test - public void D_delete() - { - userService.delete(13); - assertEquals(4, - userService.findAll(Pageable.unpaged()) - .size()); - } - - @Test(expected = ResourceNotFoundException.class) - public void DA_notFoundDelete() - { - userService.delete(100); - assertEquals(4, - userService.findAll(Pageable.unpaged()) - .size()); - } - - @Test - public void E_findByUsername() - { - assertEquals("admin", - userService.findByName("admin") - .getUsername()); - } - - @Test(expected = ResourceNotFoundException.class) - public void AA_findByUsernameNotfound() - { - assertEquals("admin", - userService.findByName("turtle") - .getUsername()); - } - - @Test - public void AB_findByNameContaining() - { - assertEquals(4, - userService.findByNameContaining("a", - Pageable.unpaged()) - .size()); - } - - @Test - public void F_save() - { - ArrayList datas = new ArrayList<>(); - User u2 = new User("tiger", - "ILuvMath!", - "tiger@school.lambda", - datas); - u2.getUseremails() - .add(new Useremail(u2, - "tiger@tiger.local")); - - User saveU2 = userService.save(u2); - - System.out.println("*** DATA ***"); - System.out.println(saveU2); - System.out.println("*** DATA ***"); - - assertEquals("tiger@tiger.local", - saveU2.getUseremails() - .get(0) - .getUseremail()); - } - - @Test(expected = ResourceFoundException.class) - public void FA_saveResourceFound() - { - ArrayList datas = new ArrayList<>(); - User u2 = new User("cinnamon", - "ILuvMath!", - "cinnamon@school.lambda", - datas); - u2.getUseremails() - .add(new Useremail(u2, - "tiger@tiger.local")); - - User saveU2 = userService.save(u2); - - System.out.println("*** DATA ***"); - System.out.println(saveU2); - System.out.println("*** DATA ***"); - - assertEquals("tiger@tiger.local", - saveU2.getUseremails() - .get(0) - .getUseremail()); - } - - @Transactional - @WithUserDetails("cinnamon") - @Test - public void G_update() - { - ArrayList datas = new ArrayList<>(); - User u2 = new User("cinnamon", - "password", - "cinnamon@school.lambda", - datas); - u2.getUseremails() - .add(new Useremail(u2, - "cinnamon@mymail.thump")); - u2.getUseremails() - .add(new Useremail(u2, - "hops@mymail.thump")); - u2.getUseremails() - .add(new Useremail(u2, - "bunny@email.thump")); - - User updatedu2 = userService.update(u2, - 7, - false); - - System.out.println("*** DATA ***"); - System.out.println(updatedu2); - System.out.println("*** DATA ***"); - - int checking = updatedu2.getUseremails() - .size() - 1; - assertEquals("bunny@email.thump", - updatedu2.getUseremails() - .get(checking) - .getUseremail()); - } - - @Transactional - @WithUserDetails("cinnamon") - @Test(expected = ResourceFoundException.class) - public void GA_updateWithUserRole() - { - Role r2 = new Role("user"); - - ArrayList datas = new ArrayList<>(); - User u2 = new User("cinnamon", - "password", - "cinnamon@school.lambda", - datas); - datas.add(new UserRoles(u2, - r2)); - u2.getUseremails() - .add(new Useremail(u2, - "cinnamon@mymail.thump")); - u2.getUseremails() - .add(new Useremail(u2, - "hops@mymail.thump")); - u2.getUseremails() - .add(new Useremail(u2, - "bunny@email.thump")); - - User updatedu2 = userService.update(u2, - 7, - false); - - System.out.println("*** DATA ***"); - System.out.println(updatedu2); - System.out.println("*** DATA ***"); - - int checking = updatedu2.getUseremails() - .size() - 1; - assertEquals("bunny@email.thump", - updatedu2.getUseremails() - .get(checking) - .getUseremail()); - } - - @Transactional - @WithUserDetails("cinnamon") - @Test(expected = ResourceNotFoundException.class) - public void GB_updateNotCurrentUserNorAdmin() - { - Role r2 = new Role("user"); - - ArrayList datas = new ArrayList<>(); - User u2 = new User("cinnamon", - "password", - "cinnamon@school.lambda", - datas); - u2.getUseremails() - .add(new Useremail(u2, - "cinnamon@mymail.thump")); - u2.getUseremails() - .add(new Useremail(u2, - "hops@mymail.thump")); - u2.getUseremails() - .add(new Useremail(u2, - "bunny@email.thump")); - - User updatedu2 = userService.update(u2, - 8, - false); - - System.out.println("*** DATA ***"); - System.out.println(updatedu2); - System.out.println("*** DATA ***"); - - int checking = updatedu2.getUseremails() - .size() - 1; - assertEquals("bunny@email.thump", - updatedu2.getUseremails() - .get(checking) - .getUseremail()); - } - - @Test(expected = ResourceNotFoundException.class) - public void H_deleteUserRoleComboNotFound() - { - // testing cinnamon and user roles - userService.deleteUserRole(11, - 2); - } - - @Test(expected = ResourceNotFoundException.class) - public void HA_deleteUserRoleRoleNotFound() - { - userService.deleteUserRole(7, - 50); - } - - @Test(expected = ResourceNotFoundException.class) - public void HB_deleteUserRoleUserNotFound() - { - userService.deleteUserRole(50, - 2); - } - - @Test(expected = ResourceFoundException.class) - public void IA_addUserRoleUserRoleFound() - { - userService.addUserRole(11, - 1); - } - - @Test - public void IB_deleteUserRole() - { - userService.deleteUserRole(11, - 1); - } - - @Test(expected = ResourceNotFoundException.class) - public void IC_addUserRoleRoleNotFound() - { - userService.addUserRole(7, - 50); - } - - @Test(expected = ResourceNotFoundException.class) - public void ID_addUserRoleUserNotFound() - { - userService.addUserRole(50, - 2); - } - - @Test - public void IE_addUserRole() - { - userService.addUserRole(11, - 2); - } -} \ No newline at end of file diff --git a/oauth2/src/test/resources/application.properties b/oauth2/src/test/resources/application.properties index 3fef8ec..fcfb92c 100644 --- a/oauth2/src/test/resources/application.properties +++ b/oauth2/src/test/resources/application.properties @@ -2,8 +2,8 @@ # for a list of application.properties # Environment Variables Needed for PostgreSQL database # Which db to run -# local.run.db=H2 -local.run.db=POSTGRESQL +local.run.db=H2 +# local.run.db=POSTGRESQL # MYDBHOST # MYDBNAME # MYDBUSER From 37d41c6db24a15f181c0d86feac97fb7b1f41468 Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Mon, 18 Nov 2019 12:34:36 -0700 Subject: [PATCH 34/35] iddingauser --- .../lambdaschool/usermodel/controllers/OpenController.java | 4 +++- .../java/com/lambdaschool/usermodel/models/UserMinimum.java | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/OpenController.java b/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/OpenController.java index d58c2c2..3131e63 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/OpenController.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/OpenController.java @@ -25,6 +25,8 @@ import java.util.ArrayList; import java.util.List; + +// TODO CU 1 @Loggable @RestController @Api(tags = {"OpenEndpoint"}) @@ -80,7 +82,7 @@ public ResponseEntity addNewUser(HttpServletRequest httpServletRequest, newuser.setUsername(newminuser.getUsername()); newuser.setPassword(newminuser.getPassword()); newuser.setPrimaryemail(newminuser.getPrimaryemail()); - +//TODO CU 1a (auto defaults to user)(requires model UserMinimum) ArrayList newRoles = new ArrayList<>(); newRoles.add(new UserRoles(newuser, roleService.findByName("user"))); diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/models/UserMinimum.java b/oauth2/src/main/java/com/lambdaschool/usermodel/models/UserMinimum.java index 370d5d1..1c62804 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/models/UserMinimum.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/models/UserMinimum.java @@ -2,6 +2,8 @@ import com.lambdaschool.usermodel.logging.Loggable; +//TODO CU 2 the model + @Loggable public class UserMinimum { From 08e5c4d77d89b23877fd953292a53843186f9fba Mon Sep 17 00:00:00 2001 From: Albert Yakubov <46692454+albert-yakubov@users.noreply.github.com> Date: Tue, 26 Nov 2019 18:17:52 -0700 Subject: [PATCH 35/35] Update BookController.java Made sure that all required endpoints work and they do --- .../com/lambdaschool/usermodel/controllers/BookController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/BookController.java b/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/BookController.java index c460268..8a27f87 100644 --- a/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/BookController.java +++ b/oauth2/src/main/java/com/lambdaschool/usermodel/controllers/BookController.java @@ -21,7 +21,7 @@ public class BookController { private static final Logger logger = LoggerFactory.getLogger(BookController.class); - // http://localhost:2021/books/books + // http://localhost:2021/book/books @GetMapping(value = "/books", produces = "application/json") ResponseEntity getBooks(){ logger.info("books/books Accessed");