Dadinho2000
Dadinho2000

Reputation: 117

Soft Deletion in a Java REST API with Spring Boot , Spring Data JPA and MySQL

I've got implemented a delete operation, which deletes a vendor, according to its Id(vendorId). But now, I need to implement a soft deletion instead. I thought about adding an attribute and all, but everything's going in the wrong direction I think. My CloudVendor.java

    package com.thinkcon.demo.model;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name="cloud_vendor_info")

public class CloudVendor
{
    @Id
    private String vendorId;
    private String vendorName;
    private String vendorAddress;
    private String vendorPhoneNumber;

    

    public CloudVendor(String vendorId, String vendorName, String vendorAddress, String vendorPhoneNumber) {
        this.vendorId = vendorId;
        this.vendorName = vendorName;
        this.vendorAddress = vendorAddress;
        this.vendorPhoneNumber = vendorPhoneNumber;
    }

    

    public CloudVendor() {

    }



    public String getVendorId() {
        return vendorId;
    }
    public String getVendorName() {
        return vendorName;
    }
    public String getVendorAddress() {
        return vendorAddress;
    }
    public String getVendorPhoneNumber() {
        return vendorPhoneNumber;
    }
    public void setVendorId(String vendorId) {
        this.vendorId = vendorId;
    }
    public void setVendorName(String vendorName) {
        this.vendorName = vendorName;
    }
    public void setVendorAddress(String vendorAddress) {
        this.vendorAddress = vendorAddress;
    }
    public void setVendorPhoneNumber(String vendorPhoneNumber) {
        this.vendorPhoneNumber = vendorPhoneNumber;
    }
    
    
}

My CloudVendorService.java

package com.thinkcon.demo.service;
import java.util.List;

import com.thinkcon.demo.model.CloudVendor;

public interface CloudVendorService {
    public String createCloudVendor(CloudVendor cloudVendor);
    public String updateCloudVendor(CloudVendor cloudVendor);
    public String deleteCloudVendor(String cloudVendorId);
    public CloudVendor getCloudVendor(String CloundVendorId);
    public List<CloudVendor> getAllCloudVendors();



    
}

My cloudVendorServiceimpl.java

package com.thinkcon.demo.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.thinkcon.demo.model.CloudVendor;
import com.thinkcon.demo.repository.CloudVendorRepository;
import com.thinkcon.demo.service.CloudVendorService;

@Service
public class CloudVendorServiceimpl implements CloudVendorService {

    CloudVendorRepository cloudVendorRepository;

    public CloudVendorServiceimpl(CloudVendorRepository cloudVendorRepository){
        this.cloudVendorRepository = cloudVendorRepository;
    }

    @Override
    public String createCloudVendor(CloudVendor cloudVendor) {
        cloudVendorRepository.save(cloudVendor);
        return "Success";
    }

    @Override
    public String updateCloudVendor(CloudVendor cloudVendor) {
        cloudVendorRepository.save(cloudVendor);
        return "Success";
    }

    @Override
    public String deleteCloudVendor(String cloudVendorId) {
        cloudVendorRepository.deleteById(cloudVendorId);
        return "Success";
    }

    @Override
    public CloudVendor getCloudVendor(String CloundVendorId) {
        return cloudVendorRepository.findById(CloundVendorId).get();
    }

    @Override
    public List<CloudVendor> getAllCloudVendors() {
        return cloudVendorRepository.findAll();
    }
    
}

And finally, my CloudVendorController.java :

package com.thinkcon.demo.controller;

import com.thinkcon.demo.model.CloudVendor;
import com.thinkcon.demo.service.CloudVendorService;

import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/cloudvendor")
public class CloudVendorController {
    
    CloudVendorService cloudVendorService;

    public CloudVendorController(CloudVendorService cloudVendorService)
    {
        this.cloudVendorService = cloudVendorService;
    }

    @GetMapping("{vendorId}")
    public CloudVendor getCloudVendorDetails(@PathVariable("vendorId") String vendorId){
        return cloudVendorService.getCloudVendor(vendorId);
    }

    @GetMapping()
    public List<CloudVendor> getAllCloudVendorDetails(){
        return cloudVendorService.getAllCloudVendors();
    }

    @PostMapping
    public String createCloudVendorDetails(@RequestBody CloudVendor cloudVendor)
    {
        
        cloudVendorService.createCloudVendor(cloudVendor);
        return "CloudVendor Created Successfully";
    }

    @PutMapping
    public String updateCloudVendorDetails(@RequestBody CloudVendor cloudVendor)
    {
        cloudVendorService.updateCloudVendor(cloudVendor);
        return "CloudVendor updated Successfully";
    }

    @DeleteMapping("{vendorId}")
    public String deleteCloudVendorDetails(@PathVariable("vendorId")String vendorId)
    {
        cloudVendorService.deleteCloudVendor(vendorId);
         return "CloudVendor deleted Successfully";
    }
}

Instead of a delete operation, a need a soft one, one that marks a vendor and 'archive' them.

Upvotes: 0

Views: 390

Answers (1)

Burak Ozmen
Burak Ozmen

Reputation: 118

You can add a new column to "cloud_vendor_info" that holds active/passive status. But be careful that whole logic should be refactored based on how passive cloud vendors will act.

Also, you should create a new table called "cloud_vendor_info_archive" and save the soft deleted/marked CloudVendor there if you need a history.

Upvotes: 1

Related Questions