fix fansNumber
This commit is contained in:
parent
aa9798b68a
commit
b4a3786b33
@ -21,6 +21,7 @@ import com.ip.library.controllers.authors.AuthorEntity;
|
||||
import com.ip.library.controllers.authors_books.AuthorsBooksEntity;
|
||||
import com.ip.library.controllers.favorites.FavoriteEntity;
|
||||
import com.ip.library.controllers.types.TypeEntity;
|
||||
import com.ip.library.controllers.users.UserEntity;
|
||||
|
||||
@Entity
|
||||
@Table(name = "books")
|
||||
@ -39,7 +40,7 @@ public class BookEntity extends BaseEntity {
|
||||
private Set<FavoriteEntity> fans = new HashSet<>();
|
||||
@Transient
|
||||
private boolean isChosen = false;
|
||||
@Transient
|
||||
@Column(nullable = false)
|
||||
private int fansNumber = 0;
|
||||
|
||||
public BookEntity() {
|
||||
@ -51,6 +52,10 @@ public class BookEntity extends BaseEntity {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public BookEntity(boolean isChosen) {
|
||||
this.isChosen = isChosen;
|
||||
}
|
||||
|
||||
public TypeEntity getType() {
|
||||
return type;
|
||||
}
|
||||
@ -110,6 +115,23 @@ public class BookEntity extends BaseEntity {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean addFan(UserEntity user) {
|
||||
FavoriteEntity entity = new FavoriteEntity(user, this);
|
||||
boolean result = fans.add(entity);
|
||||
if (!user.getFavorites().contains(entity)) {
|
||||
user.getFavorites().add(entity);
|
||||
}
|
||||
fansNumber++;
|
||||
return result;
|
||||
}
|
||||
|
||||
public void removeFan(FavoriteEntity favorite) {
|
||||
if (fans.contains(favorite)) {
|
||||
fans.remove(favorite);
|
||||
}
|
||||
fansNumber--;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, name, type);
|
||||
|
@ -80,7 +80,6 @@ public class BookService {
|
||||
if (favorites.contains(book)) {
|
||||
book.setIsChosen(true);
|
||||
}
|
||||
book.setFansNumber(book.getFans().size());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -73,11 +73,13 @@ public class UserEntity extends BaseEntity {
|
||||
}
|
||||
|
||||
public boolean addBook(BookEntity book) {
|
||||
return favorites.add(new FavoriteEntity(this, book));
|
||||
}
|
||||
|
||||
public boolean removeBook(BookEntity book) {
|
||||
return favorites.remove(new FavoriteEntity(this, book));
|
||||
FavoriteEntity entity = new FavoriteEntity(this, book);
|
||||
boolean result = favorites.add(entity);
|
||||
if (!book.getFans().contains(entity)) {
|
||||
book.getFans().add(entity);
|
||||
book.setFansNumber(book.getFansNumber() + 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -130,10 +130,12 @@ public class UserService implements UserDetailsService{
|
||||
|
||||
@Transactional
|
||||
public FavoriteEntity removeFavorite(long userId, long bookId) {
|
||||
final FavoriteEntity existsEntity = favoriteRepository.findById(new UserBookId(userId, bookId))
|
||||
.orElseThrow(() -> new IllegalArgumentException("Invalid id"));
|
||||
favoriteRepository.delete(existsEntity);
|
||||
return existsEntity;
|
||||
final FavoriteEntity favorite = favoriteRepository.findById(new UserBookId(userId, bookId))
|
||||
.orElseThrow(() -> new IllegalArgumentException("Invalid id"));
|
||||
BookEntity book = bookService.get(bookId);
|
||||
book.removeFan(favorite);
|
||||
favoriteRepository.delete(favorite);
|
||||
return favorite;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
|
@ -49,12 +49,23 @@ class FavoritesTests {
|
||||
userService.addFavorite(user1.getId(), book2.getId());
|
||||
userService.addFavorite(user2.getId(), book1.getId());
|
||||
userService.addFavorite(user2.getId(), book2.getId());
|
||||
book1 = bookService.get(book1.getId());
|
||||
book2 = bookService.get(book2.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getBookSubscribersNumberTest() {
|
||||
Assertions.assertEquals(1, bookService.getBookSubscribersNumber(book1.getId()));
|
||||
Assertions.assertEquals(2, bookService.getBookSubscribersNumber(book2.getId()));
|
||||
void getFansNumberTest() {
|
||||
Assertions.assertEquals(1, book1.getFansNumber());
|
||||
Assertions.assertEquals(2, book2.getFansNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeFavoriteTest() {
|
||||
Assertions.assertTrue(userService.getUserFavorities(user2.getId()).contains(book1));
|
||||
userService.removeFavorite(user2.getId(), book1.getId());
|
||||
Assertions.assertTrue(!userService.getUserFavorities(user2.getId()).contains(book1));
|
||||
book1 = bookService.get(book1.getId());
|
||||
Assertions.assertEquals(0, book1.getFansNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -64,11 +75,4 @@ class FavoritesTests {
|
||||
Assertions.assertTrue(list.contains(book1));
|
||||
Assertions.assertTrue(list.contains(book2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeFavoriteTest() {
|
||||
Assertions.assertTrue(userService.getUserFavorities(user2.getId()).contains(book1));
|
||||
userService.removeFavorite(user2.getId(), book1.getId());
|
||||
Assertions.assertTrue(!userService.getUserFavorities(user2.getId()).contains(book1));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user