feat: add organization entity, repository, and service

This commit is contained in:
dualfroz committed 2026-08-25 14:21:08 +02:00
1 parent a33c1d6da4
commit 7b3bdd4075
3 files changed
+119

No files matched your search

@@ -0,0 +1,53 @@
package com.slotline.organization;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.time.Instant;
@Entity
@Table(name = "organization")
public class Organization {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 128)
private String name;
@Column(nullable = false, unique = true, length = 64)
private String slug;
@Column(name = "created_at", nullable = false)
private Instant createdAt;
protected Organization() {
}
public Organization(String name, String slug, Instant createdAt) {
this.name = name;
this.slug = slug;
this.createdAt = createdAt;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getSlug() {
return slug;
}
public Instant getCreatedAt() {
return createdAt;
}
}
@@ -0,0 +1,15 @@
package com.slotline.organization;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
import java.util.Optional;
public interface OrganizationRepository extends JpaRepository<Organization, Long> {
Optional<Organization> findBySlug(String slug);
boolean existsBySlug(String slug);
List<Organization> findAllByOrderByCreatedAtAsc();
}
@@ -0,0 +1,51 @@
package com.slotline.organization;
import com.slotline.common.NotFoundException;
import com.slotline.common.SlugTakenException;
import com.slotline.organization.dto.OrganizationRequest;
import com.slotline.organization.dto.OrganizationResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.Instant;
import java.util.List;
@Service
public class OrganizationService {
public static final String defaultSlug = "default";
private final OrganizationRepository organizations;
public OrganizationService(OrganizationRepository organizations) {
this.organizations = organizations;
}
@Transactional
public OrganizationResponse create(OrganizationRequest request) {
if (organizations.existsBySlug(request.slug())) {
throw new SlugTakenException(request.slug());
}
Organization organization = new Organization(request.name(), request.slug(), Instant.now());
return OrganizationResponse.from(organizations.save(organization));
}
@Transactional(readOnly = true)
public List<OrganizationResponse> findAll() {
return organizations.findAllByOrderByCreatedAtAsc().stream()
.map(OrganizationResponse::from)
.toList();
}
@Transactional(readOnly = true)
public Organization require(Long id) {
return organizations.findById(id)
.orElseThrow(() -> new NotFoundException("Organization " + id + " not found"));
}
@Transactional(readOnly = true)
public Organization requireBySlug(String slug) {
return organizations.findBySlug(slug)
.orElseThrow(() -> new NotFoundException("Organization '" + slug + "' not found"));
}
}