public async Task <IActionResult> CreateAuthor(AuthorPostDTO authorToBeCreated) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Check if author already exists. Author existingAuthor = await _db.Authors.FirstOrDefaultAsync(a => a.Praenomen == authorToBeCreated.Praenomen && a.Nomen == authorToBeCreated.Nomen && a.Cognomen == authorToBeCreated.Cognomen); if (existingAuthor != null) { return(Conflict(new { message = _errorMessageAuthorExists })); } try { int userId = (int)_userCtx.GetId(); Author newAuthor = AuthorPostDTO.ToModel(authorToBeCreated, userId); await _db.Authors.AddAsync(newAuthor); await _db.SaveChangesAsync(); string locationUri = $"{_baseUrl}/api/v1/authors/{newAuthor.AuthorId}"; // Prepare response. AuthorGetDTO newAuthorResponse = AuthorGetDTO.FromModel(newAuthor); // Trigger webhook for new author event. TriggerAuthorWebhook(Event.NewAuthor, newAuthorResponse, userId); return(Created(locationUri, AuthorGetDTO.FromModel(newAuthor))); } catch (DbUpdateException) { return(StatusCode(StatusCodes.Status500InternalServerError, _errorMessageSavingData)); } }
public async Task <IActionResult> CreateWebhook(WebhookPostDTO webhookToBeCreated) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Check if webhook already exists. Webhook webhook = await _db.Webhooks .FirstOrDefaultAsync(w => w.CallbackURL == webhookToBeCreated.CallbackURL && w.Event == webhookToBeCreated.Event); if (webhook != null) { return(Conflict(new { message = _errorMessageWebhookExists })); } try { int userId = (int)_userCtx.GetId(); Webhook newWebhook = WebhookPostDTO.ToModel(webhookToBeCreated, userId); await _db.Webhooks.AddAsync(newWebhook); await _db.SaveChangesAsync(); string locationUri = $"{_baseUrl}/api/v1/user/webhooks/{newWebhook.WebhookId}"; return(Created(locationUri, WebhookGetDTO.FromModel(newWebhook))); } catch (DbUpdateException) { return(StatusCode(StatusCodes.Status500InternalServerError, _errorMessageSavingData)); } }
public async Task <IActionResult> UpdateWork(int id, WorkPutDTO workToBeUpdated) { // Check that model is valid, and also that id in URI matches id in Body. if (!ModelState.IsValid || id != workToBeUpdated.WorkId) { return(BadRequest(ModelState)); } // Check that work by that ID exists. Work existingWork = await _db.Works .Where(w => w.WorkId == id) .FirstOrDefaultAsync(); if (existingWork == null) { return(NotFound()); } // Check if author exists. Author existingAuthor = await _db.Authors .FirstOrDefaultAsync(a => a.AuthorId == workToBeUpdated.AuthorId); if (existingAuthor == null) { return(BadRequest(new { message = _errorMessageAuthorNotExists })); } // Check if work with that title already exists. Work existingWorkWithTitle = await _db.Works .FirstOrDefaultAsync(w => w.Title == workToBeUpdated.Title); if (existingWorkWithTitle != null) { return(Conflict(new { message = _errorMessageWorkExists })); } try { int userId = (int)_userCtx.GetId(); // First update work. _db.Entry(existingWork).CurrentValues.SetValues(workToBeUpdated); existingWork.LastEditedBy = userId; existingWork.LastEditedDate = DateTime.Now; await _db.SaveChangesAsync(); // Prepare response. WorkGetDTO editedWorkResponse = WorkGetDTO.FromModel(existingWork); // Trigger webhook for edited work. TriggerWorkWebhook(Event.EditedWork, editedWorkResponse, userId); return(Ok(editedWorkResponse)); } catch (DbUpdateException) { return(StatusCode(StatusCodes.Status500InternalServerError, _errorMessageSavingData)); } }