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?