Skip to content
Snippets Groups Projects
Commit b23ef571 authored by Charly Maeder's avatar Charly Maeder
Browse files

Add controller to remove catalog in mongodb

parent 7e579524
No related branches found
No related tags found
1 merge request!6Feature/clean catalog
package com.smartharvester.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;
import com.smartharvester.dao.CatalogDaoRepository;
import com.smartharvester.dao.OpenApiDaoRepository;
import com.smartharvester.dao.UserDaoRepository;
import com.smartharvester.exception.ResourceNotFoundException;
import com.smartharvester.model.login.response.MessageResponse;
import com.smartharvester.model.user.Catalog;
import com.smartharvester.model.user.SmartHarvesterUser;
import com.smartharvester.service.UserDaoSevice;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@CrossOrigin(origins = "*")
@RestController
@Tag(name = "SmartHarvester catalog", description = "Catalog management")
@RequestMapping("/harvester/api/catalogs")
public class SmartHarvesterCatalogController {
@Autowired
private CatalogDaoRepository catalogDoaRepository;
@Autowired
private OpenApiDaoRepository openApiDaoRepository;
@DeleteMapping("/{catId}")
public ResponseEntity<?> registerUser(@PathVariable("catId") String catId) {
Catalog catalog = catalogDoaRepository.findByCatId(catId);
if (catalog == null) {
return ResponseEntity
.badRequest()
.body(new MessageResponse("Error: Catalog not found!",HttpStatus.NOT_FOUND));
}
if (openApiDaoRepository.existsByCatalogId(catId)) {
openApiDaoRepository.delete(openApiDaoRepository.findByUUID(catId));
}
catalogDoaRepository.deleteByCatId(catalog.getCatId());
return ResponseEntity.ok(new MessageResponse("Catalog delelted succefully!",HttpStatus.OK));
}
}
......@@ -4,14 +4,18 @@ import java.util.List;
import com.smartharvester.model.user.Catalog;
import org.springframework.data.mongodb.repository.DeleteQuery;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CatalogDaoRepository extends MongoRepository<Catalog, Integer> {
public interface CatalogDaoRepository extends MongoRepository<Catalog, String> {
List<Catalog> findByUserUuid(String userUuid);
Catalog findByCatId(String catId);
@DeleteQuery
public void deleteByCatId(String catId);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment