ДА ГОСПОДИ неужели починила...

This commit is contained in:
Елена Бакальская 2024-04-29 11:22:37 +04:00
parent a10af3ab43
commit 2b3e8e4001
10 changed files with 52151 additions and 121 deletions

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -55,9 +55,8 @@ public class FavoriteController {
}
@GetMapping("/{id}")
public FavoriteDto get(@PathVariable(name = "userId") Integer userId,
@PathVariable(name = "id") Integer id) {
return toDto(favoriteService.get(userId, id));
public FavoriteDto get(@PathVariable(name = "id") Integer id) {
return toDto(favoriteService.get(id));
}
@PostMapping
@ -66,15 +65,13 @@ public class FavoriteController {
}
@PutMapping("/{id}")
public FavoriteDto update(@PathVariable(name = "userId") Integer userId,
@PathVariable(name = "id") Integer id,
public FavoriteDto update(@PathVariable(name = "id") Integer id,
@RequestBody FavoriteDto dto) {
return toDto(favoriteService.update(userId, id, toEntity(dto)));
return toDto(favoriteService.update(id, toEntity(dto)));
}
@DeleteMapping("/{id}")
public FavoriteDto delete(@PathVariable(name = "userId") Integer userId,
@PathVariable(name = "id") Integer id) {
return toDto(favoriteService.delete(userId, id));
public FavoriteDto delete(@PathVariable(name = "id") Integer id) {
return toDto(favoriteService.delete(id));
}
}

View File

@ -1,6 +1,7 @@
package com.example.backend.favorites.service;
import java.util.List;
import java.util.stream.StreamSupport;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -8,28 +9,26 @@ import org.springframework.transaction.annotation.Transactional;
import com.example.backend.core.errors.NotFoundException;
import com.example.backend.favorites.model.FavoriteEntity;
import com.example.backend.favorites.repository.FavoriteRepository;
import com.example.backend.users.service.UserService;
@Service
public class FavoriteService {
private final FavoriteRepository repository;
private final UserService userService;
public FavoriteService(FavoriteRepository repository, UserService userService) {
public FavoriteService(FavoriteRepository repository) {
this.repository = repository;
this.userService = userService;
}
@Transactional(readOnly = true)
public List<FavoriteEntity> getAll(Integer userId) {
userService.get(userId);
if (userId == 0) {
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
}
return repository.findByUserId(userId);
}
@Transactional(readOnly = true)
public FavoriteEntity get(Integer userId, Integer id) {
userService.get(userId);
return repository.findOneByUserIdAndId(userId, id).orElseThrow(() -> new NotFoundException(id));
public FavoriteEntity get(Integer id) {
return repository.findById(id).orElseThrow(() -> new NotFoundException(id));
}
@Transactional
@ -38,16 +37,16 @@ public class FavoriteService {
}
@Transactional
public FavoriteEntity update(Integer userId, Integer id, FavoriteEntity entity) {
final FavoriteEntity exisEntity = get(userId, id);
public FavoriteEntity update(Integer id, FavoriteEntity entity) {
final FavoriteEntity exisEntity = get(id);
exisEntity.setUser(entity.getUser());
exisEntity.setMovie(entity.getMovie());
return repository.save(exisEntity);
}
@Transactional
public FavoriteEntity delete(Integer userId, Integer id) {
final FavoriteEntity exisEntity = get(userId, id);
public FavoriteEntity delete(Integer id) {
final FavoriteEntity exisEntity = get(id);
repository.delete(exisEntity);
return exisEntity;
}

View File

@ -56,9 +56,8 @@ public class ViewedController {
}
@GetMapping("/{id}")
public ViewedDto get(@PathVariable(name = "userId") Integer userId,
@PathVariable(name = "id") Integer id) {
return toDto(viewedService.get(userId, id));
public ViewedDto get(@PathVariable(name = "id") Integer id) {
return toDto(viewedService.get(id));
}
@PostMapping
@ -67,15 +66,13 @@ public class ViewedController {
}
@PutMapping("/{id}")
public ViewedDto update(@PathVariable(name = "userId") Integer userId,
@PathVariable(name = "id") Integer id,
public ViewedDto update(@PathVariable(name = "id") Integer id,
@RequestBody ViewedDto dto) {
return toDto(viewedService.update(userId, id, toEntity(dto)));
return toDto(viewedService.update(id, toEntity(dto)));
}
@DeleteMapping("/{id}")
public ViewedDto delete(@PathVariable(name = "userId") Integer userId,
@PathVariable(name = "id") Integer id) {
return toDto(viewedService.delete(userId, id));
public ViewedDto delete(@PathVariable(name = "id") Integer id) {
return toDto(viewedService.delete(id));
}
}

View File

@ -1,35 +1,34 @@
package com.example.backend.viewed.service;
import java.util.List;
import java.util.stream.StreamSupport;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.backend.core.errors.NotFoundException;
import com.example.backend.users.service.UserService;
import com.example.backend.viewed.model.ViewedEntity;
import com.example.backend.viewed.repository.ViewedRepository;
@Service
public class ViewedService {
private final ViewedRepository repository;
private final UserService userService;
public ViewedService(ViewedRepository repository, UserService userService) {
public ViewedService(ViewedRepository repository) {
this.repository = repository;
this.userService = userService;
}
@Transactional(readOnly = true)
public List<ViewedEntity> getAll(Integer userId) {
userService.get(userId);
if (userId == 0) {
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
}
return repository.findByUserId(userId);
}
@Transactional(readOnly = true)
public ViewedEntity get(Integer userId, Integer id) {
userService.get(userId);
return repository.findOneByUserIdAndId(userId, id).orElseThrow(() -> new NotFoundException(id));
public ViewedEntity get(Integer id) {
return repository.findById(id).orElseThrow(() -> new NotFoundException(id));
}
@Transactional
@ -38,16 +37,16 @@ public class ViewedService {
}
@Transactional
public ViewedEntity update(Integer userId, Integer id, ViewedEntity entity) {
final ViewedEntity exisEntity = get(userId, id);
public ViewedEntity update(Integer id, ViewedEntity entity) {
final ViewedEntity exisEntity = get(id);
exisEntity.setUser(entity.getUser());
exisEntity.setMovie(entity.getMovie());
return repository.save(exisEntity);
}
@Transactional
public ViewedEntity delete(Integer userId, Integer id) {
final ViewedEntity exisEntity = get(userId, id);
public ViewedEntity delete(Integer id) {
final ViewedEntity exisEntity = get(id);
repository.delete(exisEntity);
return exisEntity;
}

View File

@ -1,80 +1,103 @@
// package com.example.backend;
package com.example.backend;
// import org.junit.jupiter.api.TestMethodOrder;
// import org.junit.jupiter.api.Assertions;
// import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
// import org.junit.jupiter.api.Order;
// import org.junit.jupiter.api.Test;
// import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.boot.test.context.SpringBootTest;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
// import com.example.backend.categories.model.CategorieEntity;
// import com.example.backend.core.errors.NotFoundException;
// import com.example.backend.favorites.model.FavoriteEntity;
// import com.example.backend.favorites.service.FavoriteService;
// import com.example.backend.movies.model.MovieEntity;
// import com.example.backend.users.model.UserEntity;
import com.example.backend.categories.model.CategorieEntity;
import com.example.backend.categories.service.CategorieService;
import com.example.backend.core.errors.NotFoundException;
import com.example.backend.favorites.model.FavoriteEntity;
import com.example.backend.favorites.service.FavoriteService;
import com.example.backend.movies.model.MovieEntity;
import com.example.backend.movies.service.MovieService;
import com.example.backend.users.model.UserEntity;
import com.example.backend.users.service.UserService;
// @SpringBootTest
// @TestMethodOrder(OrderAnnotation.class)
// public class FavoriteServiceTests {
// @Autowired
// private FavoriteService favoriteService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
public class FavoriteServiceTests {
// @Test
// void getTest() {
// Assertions.assertThrows(NotFoundException.class, () ->
// favoriteService.get(0));
// }
@Autowired
private FavoriteService favoriteService;
// @Test
// @Order(1)
// void createTest() {
// final CategorieEntity cat1 = new CategorieEntity(1, "Драма", "");
private FavoriteEntity lastFavorite;
private FavoriteEntity lastFavorite2;
// final MovieEntity mov1 = new MovieEntity(1, cat1, "Хатико", "какой-то фильм",
// "", "");
// final MovieEntity mov2 = new MovieEntity(2, cat1, "Унесеснные призраками",
// "какой-то фильм", "", "");
private CategorieEntity cat1;
private MovieEntity mov1;
private MovieEntity mov2;
private UserEntity us1;
// final UserEntity us1 = new UserEntity(1, "elina", "123", true);
@Autowired
private CategorieService categorieService;
// favoriteService.create(new FavoriteEntity(1, us1, mov1));
// final FavoriteEntity lastFavorite = favoriteService.create(new
// FavoriteEntity(2, us1, mov2));
@Autowired
private MovieService movieService;
// Assertions.assertEquals(lastFavorite, favoriteService.get(2));
// Assertions.assertEquals(2, favoriteService.getAll(1).size());
// }
@Autowired
private UserService userService;
// @Test
// @Order(2)
// void update() {
@BeforeEach
void createData() {
removeData();
// final CategorieEntity cat1 = new CategorieEntity(1, "Драма", "");
cat1 = categorieService.create(new CategorieEntity(null, "Драма", "123"));
// final MovieEntity mov2 = new MovieEntity(1, cat1, "Унесеснные призраками",
// "какой-то фильм", "", "");
mov1 = movieService.create(new MovieEntity(null, cat1, "Хатико", "какой-то фильм",
"1", "1"));
mov2 = movieService.create(new MovieEntity(null, cat1, "Унесеснные призраками",
"2", "2", "2"));
// final UserEntity us1 = new UserEntity(1, "elina", "123", true);
us1 = userService.create(new UserEntity(null, "elina", "123", true));
// final FavoriteEntity fav = new FavoriteEntity(1, us1, mov2);
lastFavorite = favoriteService.create(new FavoriteEntity(null, us1, mov1));
lastFavorite2 = favoriteService.create(new FavoriteEntity(null, us1, mov2));
}
// final FavoriteEntity oldFav = favoriteService.get(1);
@AfterEach
void removeData() {
favoriteService.getAll(0).forEach(fv -> favoriteService.delete(fv.getId()));
userService.getAll().forEach(u -> userService.delete(u.getId()));
movieService.getAll(0).forEach(item -> movieService.delete(item.getId()));
categorieService.getAll().forEach(item -> categorieService.delete(item.getId()));
}
// final FavoriteEntity newFav = favoriteService.update(2, fav);
@Test
void getTest() {
Assertions.assertThrows(NotFoundException.class, () -> favoriteService.get(0));
}
// Assertions.assertNotEquals(oldFav.getMovie().getName(),
// newFav.getMovie().getName());
@Test
void createTest() {
Assertions.assertEquals(2, favoriteService.getAll(us1.getId()).size());
}
// }
@Test
void update() {
final FavoriteEntity fav = favoriteService.create(new FavoriteEntity(null, us1, mov2));
// @Test
// @Order(3)
// void delete() {
final FavoriteEntity oldFav = favoriteService.get(lastFavorite.getId());
// favoriteService.delete(1);
final FavoriteEntity newFav = favoriteService.update(lastFavorite2.getId(), fav);
// Assertions.assertEquals(1, favoriteService.getAll(1).size());
// }
// }
Assertions.assertNotEquals(oldFav.getMovie().getName(),
newFav.getMovie().getName());
}
@Test
@Order(3)
void delete() {
favoriteService.delete(lastFavorite.getId());
Assertions.assertEquals(1, favoriteService.getAll(us1.getId()).size());
}
}

View File

@ -14,12 +14,12 @@ import org.springframework.dao.DataIntegrityViolationException;
import com.example.backend.categories.model.CategorieEntity;
import com.example.backend.categories.service.CategorieService;
import com.example.backend.core.errors.NotFoundException;
import com.example.backend.favorites.model.FavoriteEntity;
import com.example.backend.favorites.service.FavoriteService;
import com.example.backend.viewed.model.ViewedEntity;
import com.example.backend.movies.model.MovieEntity;
import com.example.backend.movies.service.MovieService;
import com.example.backend.users.model.UserEntity;
import com.example.backend.users.service.UserService;
import com.example.backend.viewed.service.ViewedService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
@ -44,33 +44,33 @@ class MovieServiceTest {
private UserEntity user3;
@Autowired
private FavoriteService favoriteService;
private FavoriteEntity favorite;
private ViewedService viewedService;
@BeforeEach
void createData() {
removeData();
cat = categorieService.create(new CategorieEntity(1, "Drama", "null"));
cat = categorieService.create(new CategorieEntity(0, "Drama", "null"));
user = userService.create(new UserEntity(2, "1", "1", true));
user1 = userService.create(new UserEntity(3, "2", "2", true));
user2 = userService.create(new UserEntity(4, "3", "3", true));
user3 = userService.create(new UserEntity(5, "4", "4", true));
user = userService.create(new UserEntity(0, "1", "1", true));
user1 = userService.create(new UserEntity(0, "2", "2", true));
user2 = userService.create(new UserEntity(0, "3", "3", true));
user3 = userService.create(new UserEntity(0, "4", "4", true));
movie = movieService.create(new MovieEntity(6, cat, "1", "1", "1", "1"));
movie2 = movieService.create(new MovieEntity(7, cat, "2", "2", "2", "2"));
movie = movieService.create(new MovieEntity(0, cat, "1", "1", "1", "1"));
movie2 = movieService.create(new MovieEntity(0, cat, "2", "2", "2", "2"));
favorite = favoriteService.create(new FavoriteEntity(8, user, movie));
favoriteService.create(new FavoriteEntity(9, user1, movie));
favoriteService.create(new FavoriteEntity(10, user2, movie));
favoriteService.create(new FavoriteEntity(11, user3, movie));
viewedService.create(new ViewedEntity(0, user, movie));
viewedService.create(new ViewedEntity(0, user1, movie));
viewedService.create(new ViewedEntity(0, user2, movie));
viewedService.create(new ViewedEntity(0, user3, movie));
}
@AfterEach
void removeData() {
viewedService.getAll(0).forEach(vs -> viewedService.delete(vs.getId()));
userService.getAll().forEach(u -> userService.delete(u.getId()));
movieService.getAll(0).forEach(item -> movieService.delete(item.getId()));
categorieService.getAll().forEach(item -> categorieService.delete(item.getId()));
}

View File

@ -55,10 +55,11 @@ public class UserServiceTests {
@Order(2)
void update() {
final String newPassword = "000";
final UserEntity existEntity = userService.get(1);
final UserEntity existEntity = userService.get(firstUser.getId());
final String oldPassword = existEntity.getPassword();
final UserEntity entity = new UserEntity(1, existEntity.getUsername(), newPassword, existEntity.getIsAdmin());
final UserEntity newEntity = userService.update(1, entity);
final UserEntity entity = new UserEntity(null, existEntity.getUsername(), newPassword,
existEntity.getIsAdmin());
final UserEntity newEntity = userService.update(firstUser.getId(), entity);
Assertions.assertEquals(newPassword, newEntity.getPassword());
Assertions.assertNotEquals(oldPassword, newEntity.getPassword());
@ -67,7 +68,7 @@ public class UserServiceTests {
@Test
@Order(3)
void delete() {
userService.delete(1);
userService.delete(firstUser.getId());
Assertions.assertEquals(2, userService.getAll().size());
}
}

Binary file not shown.