mirror of
https://gitlab.com/etc404/software-engineering-project.git
synced 2026-05-10 20:52:58 +00:00
168 lines
4.5 KiB
Java
168 lines
4.5 KiB
Java
package com.example.demo.service.Impl;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import com.example.demo.dto.ImageDto;
|
|
import com.example.demo.dto.RecipeDto;
|
|
import com.example.demo.dto.RecipeIngredientDto;
|
|
import com.example.demo.dto.StepDto;
|
|
import com.example.demo.dto.TagDto;
|
|
import com.example.demo.dto.UserDto;
|
|
import com.example.demo.entity.Recipe;
|
|
import com.example.demo.entity.User;
|
|
import com.example.demo.exception.NotFoundException;
|
|
import com.example.demo.repository.RecipeRepo;
|
|
import com.example.demo.repository.UserRepo;
|
|
import com.example.demo.service.UserService;
|
|
|
|
import jakarta.transaction.Transactional;
|
|
|
|
@Service
|
|
public class UserServiceImpl implements UserService {
|
|
|
|
private UserRepo userRepository;
|
|
private RecipeRepo recipeRepository;
|
|
|
|
public UserServiceImpl(UserRepo userRepository, RecipeRepo recipeRepository) {
|
|
super();
|
|
this.userRepository = userRepository;
|
|
this.recipeRepository = recipeRepository;
|
|
}
|
|
|
|
public UserDto convertToDto(User user) {
|
|
return new UserDto(user.getId(), user.getUsername(), user.getEmail());
|
|
}
|
|
|
|
@Override
|
|
public User saveUser(User user) {
|
|
if (user.getRole() == null || user.getRole().isBlank()) {
|
|
user.setRole("ROLE_USER");
|
|
}
|
|
user.setBanned(false);
|
|
return userRepository.save(user);
|
|
}
|
|
|
|
@Override
|
|
public List<UserDto> getAllUsers() {
|
|
|
|
List<UserDto> list = new ArrayList<>();
|
|
for (User user : userRepository.findAll()) {
|
|
UserDto userDto = convertToDto(user);
|
|
list.add(userDto);
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
@Override
|
|
public UserDto getUserById(Integer Id) {
|
|
|
|
return convertToDto(userRepository.findById(Id).orElseThrow(() -> new NotFoundException("User", "id", Id)));
|
|
}
|
|
|
|
@Override
|
|
@Transactional
|
|
public UserDto saveFavorite(Integer userId, Integer recipeId) {
|
|
User existingUser = userRepository.findById(userId)
|
|
.orElseThrow(() -> new NotFoundException("User", "id", userId));
|
|
|
|
Recipe existingRecipe = recipeRepository.findById(recipeId)
|
|
.orElseThrow(() -> new NotFoundException("Recipe", "id", recipeId));
|
|
|
|
existingUser.getFavRecipes().add(existingRecipe);
|
|
userRepository.save(existingUser);
|
|
|
|
return convertToDto(existingUser);
|
|
}
|
|
|
|
@Override
|
|
public UserDto updateUser(User user, Integer Id) {
|
|
|
|
User existingUser = userRepository.findById(Id).orElseThrow(() -> new NotFoundException("User", "id", Id));
|
|
|
|
existingUser.setUsername(user.getUsername());
|
|
existingUser.setEmail(user.getEmail());
|
|
|
|
userRepository.save(existingUser);
|
|
|
|
return convertToDto(existingUser);
|
|
}
|
|
|
|
@Override
|
|
public void deleteUser(Integer Id) {
|
|
userRepository.findById(Id).orElseThrow(() -> new NotFoundException("User", "id", Id));
|
|
userRepository.deleteById(Id);
|
|
}
|
|
|
|
@Override
|
|
@Transactional
|
|
public void deleteFavorite(Integer userId, Integer recipeId) {
|
|
User existingUser = userRepository.findById(userId)
|
|
.orElseThrow(() -> new NotFoundException("User", "id", userId));
|
|
|
|
Recipe existingRecipe = recipeRepository.findById(recipeId)
|
|
.orElseThrow(() -> new NotFoundException("Recipe", "id", recipeId));
|
|
userRepository.save(existingUser);
|
|
|
|
existingUser.getFavRecipes().remove(existingRecipe);
|
|
|
|
}
|
|
|
|
@Override
|
|
public List<UserDto> getUsersByName(String name) {
|
|
List<User> users = userRepository.findByUsernameContainingIgnoreCase(name);
|
|
|
|
if (users.isEmpty()) {
|
|
throw new NotFoundException("User", "username containing", name);
|
|
}
|
|
|
|
List<UserDto> userList = new ArrayList<>();
|
|
|
|
for (User user : users) {
|
|
UserDto dto = convertToDto(user);
|
|
userList.add(dto);
|
|
}
|
|
return userList;
|
|
}
|
|
|
|
@Override
|
|
public UserDto banUser(Integer id) {
|
|
User user = userRepository.findById(id)
|
|
.orElseThrow(() -> new NotFoundException("User", "id", id));
|
|
user.setBanned(true);
|
|
userRepository.save(user);
|
|
return convertToDto(user);
|
|
}
|
|
|
|
@Override
|
|
public UserDto unbanUser(Integer id) {
|
|
User user = userRepository.findById(id)
|
|
.orElseThrow(() -> new NotFoundException("User", "id", id));
|
|
user.setBanned(false);
|
|
userRepository.save(user);
|
|
return convertToDto(user);
|
|
}
|
|
|
|
@Override
|
|
public UserDto makeAdmin(Integer id) {
|
|
User user = userRepository.findById(id)
|
|
.orElseThrow(() -> new NotFoundException("User", "id", id));
|
|
user.setRole("ROLE_ADMIN");
|
|
userRepository.save(user);
|
|
return convertToDto(user);
|
|
}
|
|
|
|
@Override
|
|
public UserDto makeUser(Integer id) {
|
|
User user = userRepository.findById(id)
|
|
.orElseThrow(() -> new NotFoundException("User", "id", id));
|
|
user.setRole("ROLE_USER");
|
|
userRepository.save(user);
|
|
return convertToDto(user);
|
|
}
|
|
}
|