Home > Software design >  How would I find the existing objects id in the database using spring boot?
How would I find the existing objects id in the database using spring boot?

Time:02-24

The application has 2 functionalities where I can create a new script file and also edit the existing script file. The logic I have for both create and edit will basically be the same with the only major difference being that for edit instead of creating new scripts, I will have to find the existing script and override it. I am not sure how I should go finding the script by id in spring boot.

Here is my create script functionality:

Controller Class:

@Controller
@RequestMapping("/api/auth/")
public class TestServerController {
   @Autowired
   ScriptsRepository scriptsRepository;
   @Autowired
   LineItemRepository lineItemRepository;
   @Autowired
   LineItemDataRepository lineItemDataRepository;
  public TestServerController() {

  }
   @CrossOrigin(origins = "*")
   @RequestMapping(value = "createScript")
   public ResponseEntity<?> createScript(@RequestBody String body, @RequestParam String name) throws Exception {


      JSONArray script = new JSONArray(body);
      Scripts new_script = new Scripts(name);


      scriptsRepository.save(new_script);
      for (int i = 0;i<script.length();i  ) {

         JSONObject current_line_item = (JSONObject) script.get(i);

         if(!current_line_item.has("keyword")  ){
            return ResponseEntity
                  .badRequest()
                  .body(new MessageResponse("Invalid Json Object"));
         }
         String keyword = current_line_item.getString("keyword");
         LineItem new_li = new LineItem(keyword,i);
         new_li.setSid(new_script);

         lineItemRepository.save(new_li);

         int order = 0;
         var keys = current_line_item.keys();
         for (Iterator<String> it = keys; it.hasNext(); ) {
            var key = it.next();
            if(key.equals("keyword")){
               continue;
            }
            var value = current_line_item.getString(key);
            // we will need to do something about this if the column values arent strings

            LineItemData vb = new LineItemData(key,value,order);
            vb.setLid(new_li);
            lineItemDataRepository.save(vb);
            order  ;

         }

      }

      return ResponseEntity.ok(new MessageResponse("Script Uploaded Successfully"));
   }

For the edit script, I was thinking to remove Scripts new_script = new Scripts(name); and use the findbyId to find the script in the database and pass that in the scriptsRepository.save(new_script); where instead of new_script, I would just have the script file using the id. This hasn't worked for me and I'm getting errors. Is there another way to go about this?

CodePudding user response:

When you use scriptsRepository.save(script) it will save the script on your Database. But if you already have a script in the database with the same ID of the one you are passing through the parameter it will not create a new one, but just update it with the information of the one you are trying to save.

A Script controller (only with the update method), following REST API design, would be something like this:

@RestController
@RequestMapping("/scripts")
public class ScriptController {

    @Autowired
    ScriptRepository scriptRepository;

    @PutMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    public Script update(@RequestBody Script script, @PathVariable long id) {

        if (scriptRepository.findById(id).isEmpty()) {
            throw new RecordNotFoundException();
        }
        script.setId(id);
        return scriptRepository.save(script);
    }
}

To access it you would need to use the endpoint localhost:8080/scripts/{id} (in case you use the default Spring Boot localhost:8080).

You don't need to throw the exception but in case you want you would need to create a simple class like this:

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.NOT_FOUND)
public class RecordNotFoundException extends RuntimeException {

}
  • Related