How would I retrieve the ObjectId of a file , then input this Id into the Body of a POST request

27 Views Asked by At

This is my model of a User:

package com.example.justin_revature_project.models;

import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.*;

@Document("users")
public class User{
    @Id
    private ObjectId id;
    //TODO: Make UserName Unique
    private String username;
    private String password;
    private List<ObjectId> listOfSpecFileIds;
    private List<ObjectId> listOfParsedFileIds;

    public User() {
        super();
        this.listOfParsedFileIds = new ArrayList<>();
        this.listOfSpecFileIds = new ArrayList<>();
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
        this.listOfParsedFileIds = new ArrayList<>();
        this.listOfSpecFileIds = new ArrayList<>();
    }

    public User(ObjectId id, String username, String password, List<ObjectId> listOfSpecFileIds, List<ObjectId> listOfParsedFileIds) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.listOfSpecFileIds = listOfSpecFileIds;
        this.listOfParsedFileIds = listOfParsedFileIds;
    }

    public ObjectId getId() {
        return id;
    }

    public void setId(ObjectId id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<ObjectId> getListOfSpecFileIds() {
        return listOfSpecFileIds;
    }

    public void setListOfSpecFileIds(List<ObjectId> listOfSpecFileIds) {
        this.listOfSpecFileIds = listOfSpecFileIds;
    }

    public List<ObjectId> getListOfParsedFileIds() {
        return listOfParsedFileIds;
    }

    public void setListOfParsedFileIds(List<ObjectId> listOfParsedFileIds) {
        this.listOfParsedFileIds = listOfParsedFileIds;
    }
}

This is my controller for the User along with endpoints:

package com.example.justin_revature_project.controllers;

import com.example.justin_revature_project.exceptions.DuplicateUsernameException;
import com.example.justin_revature_project.exceptions.InvalidInputException;
import com.example.justin_revature_project.exceptions.UserNotFoundException;
import com.example.justin_revature_project.models.User;
import com.example.justin_revature_project.services.UserService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {
    private UserService userService;

    @Autowired
    public UserController(UserService userService){
        this.userService = userService;
    }

    /**
     * Test Connection to the Spring Server from API call
     * @return Pong within the response body
     */
    @GetMapping("/ping")
    public String ping() {
        return "pong!";
    }

    /**
     * Get the user with the given ID
     * @param id The id of the user being searched for within the database
     * @return The User with the given id
     * @throws UserNotFoundException - User with the given Id could not be found
     */
    @GetMapping("id/{id}")
    @ResponseStatus(HttpStatus.OK)
    public User getById(@PathVariable String id) throws UserNotFoundException {
        return userService.findById(new ObjectId(id));
    }

    /**
     * Get the user with the given user name
     * @param username the username of the user being searched for
     * @return The user with the given username
     * @throws UserNotFoundException - User with the given username could not be found
     */
    @GetMapping("username/{username}")
    @ResponseStatus(HttpStatus.OK)
    public User getByUsername(@PathVariable String username) throws UserNotFoundException {
        return userService.findByUsername(username);
    }

    @PostMapping("")
    @ResponseStatus(HttpStatus.ACCEPTED)
    public User postNewUser(@RequestBody User newUser) throws InvalidInputException, DuplicateUsernameException {
        return this.userService.registerUser(newUser);
    }
}


Here is the service for the User:

package com.example.justin_revature_project.services;

import com.example.justin_revature_project.exceptions.DuplicateUsernameException;
import com.example.justin_revature_project.exceptions.InvalidInputException;
import com.example.justin_revature_project.exceptions.UserNotFoundException;
import com.example.justin_revature_project.models.User;
import com.example.justin_revature_project.repositores.UserRepository;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService{
    private UserRepository userRepository;

    @Autowired
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public User findById(ObjectId id) throws UserNotFoundException {
        return userRepository.findById(id).orElseThrow(() ->
                new UserNotFoundException("User with id - " + id + " - could not be found "));
    }

    public User findByUsername(String username) throws UserNotFoundException{
        return userRepository.findByUsername(username).orElseThrow(() ->
                new UserNotFoundException("User with username - " + username + " - could not be found"));
    }

    public User registerUser(User newUser) throws DuplicateUsernameException, InvalidInputException {
        if(newUser.getUsername() == null || newUser.getUsername().isEmpty()) {
            throw new InvalidInputException("Username cannot be blank or null");
        }
        if(newUser.getPassword() == null || newUser.getPassword().isEmpty()) {
            throw new InvalidInputException("Password cannot be blank or null");
        }
        if(userRepository.existsUserByUsername(newUser.getUsername())) {
            throw new DuplicateUsernameException("User with username - " + newUser.getUsername() + " - already exists");
        }
        return userRepository.save(new User(newUser.getUsername(), newUser.getPassword()));
    }

    public User addSpecFileIdWithUserId(ObjectId specFileId, String userId) throws UserNotFoundException {
        //TODO: Exception Handling
        User userToUpdate = this.findById(new ObjectId(userId));
        userToUpdate.getListOfSpecFileIds().add(specFileId);
        return this.userRepository.save(userToUpdate);
    }

    public User addSpecFileId(ObjectId specFileId, User user){
        //TODO: Exception Handling
        user.getListOfSpecFileIds().add(specFileId);
        return this.userRepository.save(user);
    }

    public User addParsedFileId(ObjectId parsedFileId, User user){
        user.getListOfParsedFileIds().add(parsedFileId);
        return this.userRepository.save(user);
    }
}

I am trying to populate the listOfParsedFile and listOfSPecFile fields in the body of the postman request. How would I go about doing this?

This is from ChatGPT, this is the body of the request:

{
    "username": "example_username",
    "listOfParsedFile": [
        {"$oid": "6024d50b5e6ca225f0e8b5a0"},
        {"$oid": "6024d50b5e6ca225f0e8b5a1"},
        {"$oid": "6024d50b5e6ca225f0e8b5a2"}
    ]
}

And replace the oid with the actual file ObjectId's.

Any help would be appreciated.

0

There are 0 best solutions below