Untitled

mail@pastecode.io avatar
unknown
java
a month ago
3.6 kB
2
Indexable
Never
@Getter
@Setter
@NoArgsConstructor
@Accessors(chain = true)
@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"first_name", "last_name"}))
public class Member {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "student_number", unique = true)
    private Long studentNumber;

    @Column(name = "first_name", nullable = false)
    private String firstName;

    @Column(name = "last_name", nullable = false)
    private String lastName;
}

@Repository
public interface MemberRepository extends JpaRepository<Member, Long> {
}

@RequiredArgsConstructor
@Slf4j
@Service
public class MemberServiceImpl {
    private final MemberRepository repository;

    public Member createMember(MemberDto request) {
        Member member = new Member()
                .setStudentNumber(request.studentNumber())
                .setFirstName(request.firstName().strip())
                .setLastName(request.lastName().strip());

        try {
            repository.save(member);
            log.info("Created new member, memberId: [{}]", member.getId());

            return member;
        } catch (DataIntegrityViolationException exception) {
            throw new EntityAlreadyExistsException(
                    "Member with given identifiers already exists. Identifiers = " +
                            "firstName: " + request.firstName() + ", " +
                            "lastName: " + request.lastName() + ", " +
                            "studentNumber: " + request.studentNumber(),
                    exception
            );
        }
    }

    public List<Member> getMembers() {
        return repository.findAll(Sort.by(Sort.Direction.ASC, "id"));
    }

    public Member getMember(Long memberId) {
        Optional<Member> memberOptional = repository.findById(memberId);

        if (memberOptional.isEmpty()) {
            throw new EntityNotFoundException("No member found for given id, memberId: " + memberId);
        }

        return memberOptional.get();
    }

    public Member updateMember(Long memberId, MemberDto request) {
        Member member = getMember(memberId)
                .setStudentNumber(request.studentNumber())
                .setFirstName(request.firstName().strip())
                .setLastName(request.lastName().strip());

        try {
            repository.save(member);
            log.info("Updated member, memberId: [{}]", memberId);

            return member;
        } catch (DataIntegrityViolationException exception) {
            throw new EntityAlreadyExistsException(
                    "Member with given identifiers already exists. Identifiers = " +
                            "firstName: " + request.firstName() + ", " +
                            "lastName: " + request.lastName(),
                    exception
            );
        }
    }

    public void deleteMember(Long memberId) {
        if (!repository.existsById(memberId)) {
            log.info("No member was found to delete, memberId: [{}]", memberId);
            return;
        }

        try {
            repository.deleteById(memberId);
            log.info("Deleted member, memberId: {{}]", memberId);
        } catch (DataIntegrityViolationException exception) {
            throw new EntityStillInUseException(
                    "Member with given id is still in use, memberId: " + memberId,
                    exception
            );
        }
    }
}
Leave a Comment