Skip to content

Commit

Permalink
added endpoint and api for deleting sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
omergunr100 committed Sep 9, 2024
1 parent ae5e947 commit 14a540c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
41 changes: 41 additions & 0 deletions NetBuddy.Server/Controllers/Execution/ExecutionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,47 @@ public async Task<IActionResult> PutSequence([FromBody] Sequence sequence)
});
}

[Authorize]
[Route("sequence/{sequenceId}")]
[HttpDelete]
public async Task<IActionResult> DeleteSequence([FromRoute] string sequenceId)
{
// validate the model state
if (!ModelState.IsValid) return BadRequest(ModelState);

var user = await _userManager.GetUserAsync(User);
if (user == null) return Unauthorized();

Guid id;
try
{
id = Guid.Parse(sequenceId);
_logger.LogInformation("Parsed sequence id successfully: {id}", id);
}
catch (Exception)
{
return BadRequest("Invalid sequence id.");
}

// check if sequence exists and if the user is its owner
await using var querySession = _store.QuerySession();
var sequence = await querySession.LoadAsync<Sequence>(id);
if (sequence != null)
{
if (sequence.Owner == null || sequence.Owner.Email != user.Email)
// user is not the owner of the sequence
return NotFound();

// user is the owner and sequence found
await using var writeSession = _store.LightweightSession();
writeSession.Delete(sequence);
writeSession.SaveChangesAsync();
return Ok();
}

return NotFound();
}

[Authorize]
[Route("presets/{sequenceId}")]
[HttpGet]
Expand Down
4 changes: 4 additions & 0 deletions netbuddy.client/src/api/sequences/sequences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ export async function SaveExecutableSequence(sequence: Sequence) {
.catch((error) => console.log(error.error))
.then((response) => response?.data as { id: string; errors: string[] });
}

export async function DeleteSequence(sequenceId: string) {
return await agent.delete(`/execution/sequence/${sequenceId}`);
}

0 comments on commit 14a540c

Please sign in to comment.