public async Task <IActionResult> DeleteEmployee([FromRoute] string id) { var errorMessage = new ErrorMessageViewModel(); var error = new { Error = errorMessage }; // Delete the user from the application user list ApplicationUser user = db.ApplicationUser.Where(e => e.Id == id).SingleOrDefault(); if (user == null) { errorMessage.Message = "Could not find user"; return(Json(error)); } // Find the employee BusinessEmployees employee = db.BusinessEmployees.Where(e => e.UserId == id).SingleOrDefault(); if (employee == null) { errorMessage.Message = "Could not find employee"; return(Json(error)); } // Remove the employee db.Remove(employee); db.Remove(user); try { db.SaveChanges(); } catch (Exception exception) { errorMessage.Message = "Could not remove employee"; return(Json(error)); } return(Accepted(employee)); }
public async Task <IActionResult> UpdateBook([FromRoute] int id, [FromBody] BookDetailsViewModel book) { var errorMessage = new ErrorMessageViewModel(); var error = new { Error = errorMessage }; List <DocumentGenres> dbGenres = db.DocumentGenres.Where(e => e.DocumentId == id).ToList(); List <DocumentAuthors> dbAuthors = db.DocumentAuthors.Where(e => e.DocumentId == id).ToList(); Documents document = db.Documents.Where(e => e.DocumentId == id).SingleOrDefault(); if (document == null) { errorMessage.Message = "Could not find book in the database"; return(Json(error)); } // Remove old entries for the authors foreach (var item in dbAuthors) { db.Remove(item); } // Remove old entries for the genres foreach (var item in dbGenres) { db.Remove(item); } try { db.SaveChanges(); } catch (Exception exception) { errorMessage.Message = "Could not remove document authors or genres"; return(Json(error)); } // Add new document authors and genres foreach (var genre in book.Genres) { // Add the document genre for each genre DocumentGenres documentGenre = new DocumentGenres { DocumentId = document.DocumentId, GenreId = genre.GenreId }; db.DocumentGenres.Add(documentGenre); try { db.SaveChanges(); } catch (Exception exception) { errorMessage.Message = "Could not add document genre to database"; return(Json(error)); } } foreach (var item in book.Authors) { var dbAuthor = db.Authors.Where(e => e.Name.Equals(item.Name, StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault(); if (dbAuthor == null) { // Add the author Authors author = new Authors { Name = item.Name }; db.Authors.Add(author); try { db.SaveChanges(); } catch (Exception exception) { errorMessage.Message = "Could not add author to the database"; return(Json(error)); } // Add the author to the books list of authors DocumentAuthors documentAuthor = new DocumentAuthors { AuthorId = author.AuthorId, DocumentId = document.DocumentId }; db.DocumentAuthors.Add(documentAuthor); try { db.SaveChanges(); } catch (Exception exception) { errorMessage.Message = "Could not add document author to the database"; return(Json(error)); } } else { // Add the author to the books list of authors DocumentAuthors documentAuthor = new DocumentAuthors { AuthorId = dbAuthor.AuthorId, DocumentId = document.DocumentId }; db.DocumentAuthors.Add(documentAuthor); try { db.SaveChanges(); } catch (Exception exception) { errorMessage.Message = "Could not add document author to the database"; return(Json(error)); } } } // Attach values document.Title = book.Title; document.ISBN = book.ISBN; document.Pages = book.Pages; document.Publisher = book.Publisher; document.PublishedDate = Convert.ToDateTime(book.PublishedDate); document.CheckedOut = book.CheckedOut; document.Edition = book.Edition; document.Description = book.Description; // Add image for book if (!String.IsNullOrWhiteSpace(book.Picture)) { var fileName = await fileController.UploadImage(book.Picture, Request); if (String.IsNullOrWhiteSpace(fileName)) { errorMessage.Message = "Image upload encountered an error"; return(Json(error)); } document.CoverImage = fileName; } // Update book information db.Update(document); try { db.SaveChanges(); } catch (Exception exception) { errorMessage.Message = "Could not update book information"; return(Json(error)); } BookDetailsViewModel bookDetails = new BookDetailsViewModel { Id = document.DocumentId, Title = document.Title, ISBN = document.ISBN, CheckedOut = document.CheckedOut, Picture = document.CoverImage, Pages = Convert.ToInt32(document.Pages), Publisher = document.Publisher, PublishedDate = document.PublishedDate.ToString("yyyy-MM-dd"), Edition = document.Edition, Description = document.Description }; return(Ok(bookDetails)); }