Skip to content
Snippets Groups Projects

Feature/publish cat id to mongo

Merged Baptiste Toulemonde requested to merge feature/publish_cat_id_to_mongo into master
5 files
+ 142
9
Compare changes
  • Side-by-side
  • Inline
Files
5
@@ -7,12 +7,17 @@ import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;
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.*;
@@ -20,7 +25,7 @@ import java.util.*;
@CrossOrigin(origins = "*")
@RestController
@Tag(name="SmartHarvester users", description = "User management")
@Tag(name = "SmartHarvester users", description = "User management")
@RequestMapping("/harvester/api")
public class SmartHarvesterUserController {
@@ -31,8 +36,12 @@ public class SmartHarvesterUserController {
@Autowired
private UserDaoRepository userDaoRepository;
@Autowired
private UserDaoSevice userService;
/**
* Method to fetch all users.
*
* @return A collection of SmarHarversterUsers
*/
@@ -44,17 +53,42 @@ public class SmartHarvesterUserController {
/**
* Method to fetch a user by email.
*
* @param email The user's email
* @return An Optional SmartHarvesterUser object
*/
@GetMapping("/user/{email}")
public ResponseEntity<Optional<SmartHarvesterUser>> getUserByEmail(@PathVariable("email") String email)
{
return Optional
.ofNullable( userDaoRepository.findByEmail(email) )
.map( user -> ResponseEntity.ok().body(user) )
.orElseGet( () -> ResponseEntity.notFound().build() );
public ResponseEntity<Optional<SmartHarvesterUser>> getUserByEmail(@PathVariable("email") String email) {
return Optional.ofNullable(userDaoRepository.findByEmail(email)).map(user -> ResponseEntity.ok().body(user))
.orElseGet(() -> ResponseEntity.notFound().build());
}
/**
* Method to get list of catalog id attached to a user
*
* @param email
* @return list of catalogs id
* @throws ResourceNotFoundException
*/
@GetMapping("user/{uuid}/catalogs")
public ResponseEntity<List<Catalog>> getCatalogs(@PathVariable("uuid") String uuid)
throws ResourceNotFoundException {
return Optional.ofNullable(userService.getCatalog(uuid)).map(cat -> ResponseEntity.ok().body(cat))
.orElseGet(() -> ResponseEntity.notFound().build());
}
@PostMapping("user/{uuid}/catalogs")
public ResponseEntity<?> addCatalogId(@PathVariable("uuid") String uuid, @RequestBody String catId) {
try {
userService.saveCatalog(catId, uuid);
return ResponseEntity.ok(new MessageResponse("cat_id " + catId + " add with success", HttpStatus.OK));
} catch (ResourceNotFoundException e) {
return ResponseEntity.badRequest()
.body(new MessageResponse(e.getMessage(), HttpStatus.BAD_REQUEST));
}
}
}
Loading