feat(services-counter): store each company's interest list
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
4c853c428e
commit
b75d636886
8 files changed
+598
-1
No files matched your search
+31
@@ -0,0 +1,31 @@
|
||||
package com.solvway.services.counter.controller;
|
||||
|
||||
import com.solvway.services.counter.dto.InterestFindByIdsRequest;
|
||||
import com.solvway.services.counter.model.Interest;
|
||||
import com.solvway.services.counter.service.InterestService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/counter/interest")
|
||||
@AllArgsConstructor
|
||||
public class InterestController {
|
||||
private final InterestService interestService;
|
||||
|
||||
@GetMapping("/findByCompany/{companyId}")
|
||||
public List<Interest> findByCompany(@PathVariable("companyId") long companyId) {
|
||||
return interestService.findByCompany(companyId);
|
||||
}
|
||||
|
||||
@PostMapping("/save")
|
||||
public Interest save(@RequestBody Interest interest) {
|
||||
return interestService.save(interest);
|
||||
}
|
||||
|
||||
@PostMapping("/findByIds")
|
||||
public List<Interest> findByIds(@RequestBody InterestFindByIdsRequest body) {
|
||||
return interestService.findByIds(body.companyId(), body.ids());
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.solvway.services.counter.dto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record InterestFindByIdsRequest(
|
||||
long companyId,
|
||||
List<Long> ids
|
||||
) {}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.solvway.services.counter.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@Table(name = "interest", uniqueConstraints = @UniqueConstraint(columnNames = {"company_id", "name"}))
|
||||
public class Interest {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private long companyId;
|
||||
|
||||
private String name;
|
||||
|
||||
private boolean active = true;
|
||||
|
||||
private int sortOrder;
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.solvway.services.counter.repo;
|
||||
|
||||
import com.solvway.services.counter.model.Interest;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface InterestRepository extends JpaRepository<Interest, Long> {
|
||||
List<Interest> findByCompanyIdOrderBySortOrderAscNameAsc(long companyId);
|
||||
|
||||
boolean existsByCompanyIdAndNameIgnoreCase(long companyId, String name);
|
||||
|
||||
List<Interest> findByIdInAndCompanyId(Collection<Long> ids, long companyId);
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.model.Interest;
|
||||
import com.solvway.services.counter.repo.InterestRepository;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class InterestService {
|
||||
private final InterestRepository interestRepository;
|
||||
|
||||
public List<Interest> findByCompany(long companyId) {
|
||||
return interestRepository.findByCompanyIdOrderBySortOrderAscNameAsc(companyId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Interest save(Interest interest) {
|
||||
String name = interest.getName();
|
||||
String trimmedName = name != null ? name.trim() : null;
|
||||
interest.setName(trimmedName);
|
||||
|
||||
// Validate name
|
||||
if (trimmedName == null || trimmedName.isEmpty()) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "REQUIRED_FIELD");
|
||||
}
|
||||
if (trimmedName.length() > 100) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "TOO_LONG");
|
||||
}
|
||||
|
||||
try {
|
||||
if (interest.getId() == null) {
|
||||
// Create: check duplicate name for this company
|
||||
if (interestRepository.existsByCompanyIdAndNameIgnoreCase(interest.getCompanyId(), trimmedName)) {
|
||||
throw new ResponseStatusException(HttpStatus.CONFLICT, "DUPLICATE_NAME");
|
||||
}
|
||||
return interestRepository.saveAndFlush(interest);
|
||||
} else {
|
||||
// Update: verify the row belongs to this company
|
||||
Interest existing = interestRepository.findById(interest.getId())
|
||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
|
||||
if (existing.getCompanyId() != interest.getCompanyId()) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
// Check duplicate name within the same company, excluding this row
|
||||
if (!existing.getName().equalsIgnoreCase(trimmedName)
|
||||
&& interestRepository.existsByCompanyIdAndNameIgnoreCase(interest.getCompanyId(), trimmedName)) {
|
||||
throw new ResponseStatusException(HttpStatus.CONFLICT, "DUPLICATE_NAME");
|
||||
}
|
||||
return interestRepository.saveAndFlush(interest);
|
||||
}
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
throw new ResponseStatusException(HttpStatus.CONFLICT, "DUPLICATE_NAME");
|
||||
}
|
||||
}
|
||||
|
||||
public List<Interest> findByIds(long companyId, List<Long> ids) {
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
return interestRepository.findByIdInAndCompanyId(ids, companyId);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user