add BrandService and enhance UserService with additional user retrieval methods
This commit is contained in:
@@ -0,0 +1,56 @@
|
|||||||
|
package fr.gameovergne.api.service.app;
|
||||||
|
|
||||||
|
import fr.gameovergne.api.model.app.Brand;
|
||||||
|
import fr.gameovergne.api.repository.app.BrandRepository;
|
||||||
|
import jakarta.transaction.Transactional;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class BrandService {
|
||||||
|
|
||||||
|
private final BrandRepository brandRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public BrandService(BrandRepository brandRepository) {
|
||||||
|
this.brandRepository = brandRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void saveBrand(Brand brand) {
|
||||||
|
if (brand.getId() == null) {
|
||||||
|
brandRepository.save(brand);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Brand> getAllBrands() {
|
||||||
|
return brandRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Brand> getBrandById(Long id) {
|
||||||
|
return brandRepository.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Brand> getBrandByName(String name) {
|
||||||
|
return brandRepository.findByName(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Optional<Brand> updateBrand(Brand brand) {
|
||||||
|
return brandRepository.findById(brand.getId()).map(existingBrand -> {
|
||||||
|
existingBrand.setName(brand.getName());
|
||||||
|
existingBrand.setPlatforms(brand.getPlatforms());
|
||||||
|
return brandRepository.save(existingBrand);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Optional<Brand> deleteBrandById(Long id) {
|
||||||
|
Optional<Brand> brand = brandRepository.findById(id);
|
||||||
|
brand.ifPresent(brandRepository::delete);
|
||||||
|
return brand;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,19 +26,25 @@ public class UserService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<User> getAllUsers() {
|
||||||
|
return userRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
public Optional<User> getUserById(Long id) {
|
public Optional<User> getUserById(Long id) {
|
||||||
return userRepository.findById(id);
|
return userRepository.findById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<User> getAllUsers() {
|
public Optional<User> getUserByUsername(String username) {
|
||||||
return userRepository.findAll();
|
return userRepository.findByUsername(username);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<User> getUserByEmail(String email) {
|
||||||
|
return userRepository.findByEmail(email);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Optional<User> updateUser(User user) {
|
public Optional<User> updateUser(User user) {
|
||||||
return userRepository.findById(user.getId()).map(existingUser -> {
|
return userRepository.findById(user.getId()).map(existingUser -> {
|
||||||
existingUser.setFirstName(user.getFirstName());
|
|
||||||
existingUser.setLastName(user.getLastName());
|
|
||||||
existingUser.setEmail(user.getEmail());
|
existingUser.setEmail(user.getEmail());
|
||||||
existingUser.setUsername(user.getUsername());
|
existingUser.setUsername(user.getUsername());
|
||||||
existingUser.setPassword(user.getPassword());
|
existingUser.setPassword(user.getPassword());
|
||||||
|
|||||||
Reference in New Issue
Block a user