Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/phone #29

Merged
merged 1 commit into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.sarapis.orservice.controller;

import com.sarapis.orservice.dto.PhoneDTO;
import com.sarapis.orservice.service.PhoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/phones")
public class PhoneController {
private final PhoneService phoneService;

@Autowired
public PhoneController(PhoneService phoneService) {
this.phoneService = phoneService;
}

@GetMapping
public ResponseEntity<List<PhoneDTO>> getAllPhones() {
List<PhoneDTO> phoneDTOs = this.phoneService.getAllPhones();
return ResponseEntity.ok(phoneDTOs);
}

@GetMapping("/{id}")
public ResponseEntity<PhoneDTO> getPhoneById(@PathVariable String id) {
PhoneDTO phoneDTO = this.phoneService.getPhoneById(id);
return ResponseEntity.ok(phoneDTO);
}

@PostMapping
public ResponseEntity<PhoneDTO> createPhone(@RequestBody PhoneDTO phoneDTO) {
if (phoneDTO.getId() == null) {
phoneDTO.setId(UUID.randomUUID().toString());
}
PhoneDTO createdPhone = this.phoneService.createPhone(phoneDTO);
return ResponseEntity.ok(createdPhone);
}

@PutMapping("/{id}")
public ResponseEntity<PhoneDTO> updatePhone(@PathVariable String id, @RequestBody PhoneDTO phoneDTO) {
PhoneDTO updatedAccessibility = this.phoneService.updatePhone(id, phoneDTO);
return ResponseEntity.ok(updatedAccessibility);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deletePhone(@PathVariable String id) {
this.phoneService.deletePhone(id);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.sarapis.orservice.repository;

import com.sarapis.orservice.entity.Attribute;
import com.sarapis.orservice.entity.Phone;
import com.sarapis.orservice.entity.Metadata;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface PhoneRepository extends JpaRepository<Phone, String> {
@Query("SELECT new Attribute(id, linkId, linkType, linkEntity, value, taxonomyTerm, label) FROM Attribute WHERE linkId = ?1")
List<Attribute> getAttributes(String organizationId);

@Query("SELECT new Metadata(id, resourceId, resourceType, lastActionDate, lastActionType, fieldName, previousValue, replacementValue, updatedBy) FROM Metadata WHERE resourceId = ?1")
List<Metadata> getMetadata(String organizationId);

@Modifying
@Transactional
@Query("DELETE FROM Attribute WHERE linkId = ?1")
void deleteAttributes(String organizationId);

@Modifying
@Transactional
@Query("DELETE FROM Metadata WHERE resourceId = ?1")
void deleteMetadata(String organizationId);
}
17 changes: 17 additions & 0 deletions src/main/java/com/sarapis/orservice/service/PhoneService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.sarapis.orservice.service;

import com.sarapis.orservice.dto.PhoneDTO;

import java.util.List;

public interface PhoneService {
List<PhoneDTO> getAllPhones();

PhoneDTO getPhoneById(String id);

PhoneDTO createPhone(PhoneDTO phoneDTO);

PhoneDTO updatePhone(String id, PhoneDTO phoneDTO);

void deletePhone(String id);
}
90 changes: 90 additions & 0 deletions src/main/java/com/sarapis/orservice/service/PhoneServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.sarapis.orservice.service;

import com.sarapis.orservice.dto.AttributeDTO;
import com.sarapis.orservice.dto.PhoneDTO;
import com.sarapis.orservice.dto.MetadataDTO;
import com.sarapis.orservice.entity.Attribute;
import com.sarapis.orservice.entity.Phone;
import com.sarapis.orservice.entity.Metadata;
import com.sarapis.orservice.repository.AttributeRepository;
import com.sarapis.orservice.repository.PhoneRepository;
import com.sarapis.orservice.repository.MetadataRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PhoneServiceImpl implements PhoneService {
private final PhoneRepository phoneRepository;
private final AttributeRepository attributeRepository;
private final MetadataRepository metadataRepository;

@Autowired
public PhoneServiceImpl(PhoneRepository phoneRepository, AttributeRepository attributeRepository, MetadataRepository metadataRepository) {
this.phoneRepository = phoneRepository;
this.attributeRepository = attributeRepository;
this.metadataRepository = metadataRepository;
}

@Override
public List<PhoneDTO> getAllPhones() {
List<PhoneDTO> phoneDTOs = this.phoneRepository.findAll().stream().map(Phone::toDTO).toList();
phoneDTOs.forEach(this::addRelatedData);
return phoneDTOs;
}

@Override
public PhoneDTO getPhoneById(String id) {
Phone phone = this.phoneRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Phone not found."));
PhoneDTO phoneDTO = phone.toDTO();
this.addRelatedData(phoneDTO);
return phoneDTO;
}

@Override
public PhoneDTO createPhone(PhoneDTO phoneDTO) {
Phone phone = this.phoneRepository.save(phoneDTO.toEntity());

for (AttributeDTO attributeDTO : phoneDTO.getAttributes()) {
this.attributeRepository.save(attributeDTO.toEntity(phone.getId()));
}

for (MetadataDTO metadataDTO : phoneDTO.getMetadata()) {
this.metadataRepository.save(metadataDTO.toEntity(phone.getId()));
}

PhoneDTO savedPhoneDTO = this.phoneRepository.save(phone).toDTO();
this.addRelatedData(savedPhoneDTO);
return savedPhoneDTO;
}

@Override
public PhoneDTO updatePhone(String id, PhoneDTO phoneDTO) {
Phone oldPhone = this.phoneRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Phone not found."));

oldPhone.setNumber(phoneDTO.getNumber());
oldPhone.setExtension(phoneDTO.getExtension());
oldPhone.setType(phoneDTO.getType());
oldPhone.setDescription(phoneDTO.getDescription());

Phone updatedPhoneDTO = this.phoneRepository.save(oldPhone);
return updatedPhoneDTO.toDTO();
}

@Override
public void deletePhone(String id) {
Phone phone = this.phoneRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Phone not found."));
this.phoneRepository.deleteAttributes(phone.getId());
this.phoneRepository.deleteMetadata(phone.getId());
this.phoneRepository.delete(phone);
}

private void addRelatedData(PhoneDTO phoneDTO) {
phoneDTO.getAttributes().addAll(this.phoneRepository.getAttributes(phoneDTO.getId()).stream().map(Attribute::toDTO).toList());
phoneDTO.getMetadata().addAll(this.phoneRepository.getMetadata(phoneDTO.getId()).stream().map(Metadata::toDTO).toList());
}
}
Loading