first commit. application working
This commit is contained in:
@ -0,0 +1,53 @@
|
||||
|
||||
package com.weighttracker.service;
|
||||
|
||||
import com.weighttracker.model.User;
|
||||
import com.weighttracker.repository.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
public User findByUsername(String username) {
|
||||
return userRepository.findByUsername(username)
|
||||
.orElseThrow(() -> new UsernameNotFoundException("Usuario no encontrado: " + username));
|
||||
}
|
||||
|
||||
public List<User> findAll() {
|
||||
return userRepository.findAll();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
public void save(User user) {
|
||||
if (user.getPassword() != null && !user.getPassword().startsWith("$2a$")) {
|
||||
user.setPassword(passwordEncoder.encode(user.getPassword()));
|
||||
}
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
||||
public User findById(Long id) {
|
||||
return userRepository.findById(id).orElseThrow();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private WeightService weightService;
|
||||
|
||||
@Transactional
|
||||
public void deleteById(Long id) {
|
||||
User user = userRepository.findById(id).orElseThrow();
|
||||
weightService.deleteByUser(user);
|
||||
userRepository.deleteById(id);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,51 @@
|
||||
package com.weighttracker.service;
|
||||
|
||||
import com.weighttracker.model.User;
|
||||
import com.weighttracker.model.WeightRecord;
|
||||
import com.weighttracker.repository.WeightRecordRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class WeightService {
|
||||
private final WeightRecordRepository repo;
|
||||
|
||||
public WeightService(WeightRecordRepository r) {
|
||||
this.repo = r;
|
||||
}
|
||||
|
||||
public WeightRecord save(WeightRecord w) {
|
||||
return repo.save(w);
|
||||
}
|
||||
|
||||
public List<WeightRecord> last10(User u) {
|
||||
return repo.findTop10ByUserOrderByDateDesc(u);
|
||||
}
|
||||
|
||||
public List<WeightRecord> findByUser(User u) {
|
||||
return repo.findByUserOrderByDateDesc(u);
|
||||
}
|
||||
|
||||
public List<WeightRecord> recordsAfter(User u, LocalDateTime d) {
|
||||
return repo.findByUserAndDateAfterOrderByDateAsc(u, d);
|
||||
}
|
||||
|
||||
public WeightRecord findById(Long id) {
|
||||
return repo.findById(id).orElseThrow();
|
||||
}
|
||||
|
||||
public void deleteById(Long id) {
|
||||
repo.deleteById(id);
|
||||
}
|
||||
|
||||
public List<WeightRecord> recordsBetween(User user, LocalDateTime start, LocalDateTime end) {
|
||||
return repo.findByUserAndDateBetweenOrderByDateAsc(user, start, end);
|
||||
}
|
||||
|
||||
public void deleteByUser(User user) {
|
||||
repo.deleteAllByUser(user);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user