// [Authorize(oAppConst.AccessPolicies.LevelTwo)] /// Ready For Test public async Task <IActionResult> Post([FromBody] oCategory newCategory) { try { /// if model validation failed if (!TryValidateModel(newCategory)) { oAppFunc.ExtractErrors(ModelState, ref ErrorsList); /// return Unprocessable Entity with all the errors return(UnprocessableEntity(ErrorsList)); } /// check the database to see if a Category with the same name exists if (await DbContext.Categories .AnyAsync(d => d.Name.Equals(newCategory.Name)).ConfigureAwait(false)) { /// extract the errors and return bad request containing the errors oAppFunc.Error(ref ErrorsList, "Category already exists."); return(StatusCode(412, ErrorsList)); } try { newCategory.ImagePath = oAppFunc.SaveImageToWWWRoot(newCategory.Name, WebHost.WebRootPath, newCategory.ImageBase64, @"Images/Categories"); } catch (Exception) { oAppFunc.Error(ref ErrorsList, "Image cannot be saved."); return(StatusCode(412, ErrorsList)); } /// else Category object is made without any errors /// Add the new Category to the EF context await DbContext.Categories.AddAsync(newCategory).ConfigureAwait(false); try { /// save the changes to the database await DbContext.SaveChangesAsync().ConfigureAwait(false); } catch (Exception) { oAppFunc.DeleteImage(newCategory.ImagePath, WebHost.WebRootPath); throw; } /// return 201 created status with the new object /// and success message return(Created("Success", newCategory)); } catch (Exception) // DbUpdateException, DbUpdateConcurrencyException { /// Add the error below to the error list and return bad request oAppFunc.Error(ref ErrorsList, oAppConst.CommonErrors.ServerError); return(StatusCode(417, ErrorsList)); } }
//[Authorize(oAppConst.AccessPolicies.LevelTwo)] /// Ready For Test public async Task <IActionResult> Delete([FromBody] oCategory category) { try { /// if the Category record with the same id is not found if (!await DbContext.Categories .AsNoTracking() .AnyAsync(c => c.Id == category.Id) .ConfigureAwait(false)) { oAppFunc.Error(ref ErrorsList, "Category not found"); return(NotFound(ErrorsList)); } /// If the category is in use by any product then do not allow delete if (await DbContext.Products .AnyAsync(c => c.Category.Id == category.Id) .ConfigureAwait(false)) { oAppFunc.Error(ref ErrorsList, "Category is in use by at least one product."); return(StatusCode(412, ErrorsList)); } try { oAppFunc.DeleteImage(category.ImagePath, WebHost.WebRootPath); } catch (Exception) { DbContext.AppLogs.Add(new oAppLog { Massage = string.Format("Image was not deleted. The path is: {0}", category.ImagePath) }); } /// else the Category is found /// now delete the Category record DbContext.Categories.Remove(category); /// save the changes to the database await DbContext.SaveChangesAsync().ConfigureAwait(false); /// return 200 OK status return(Ok($"Category '{category.Name}' was deleted")); } catch (Exception) { /// Add the error below to the error list oAppFunc.Error(ref ErrorsList, oAppConst.CommonErrors.ServerError); return(StatusCode(417, ErrorsList)); } }
// [Authorize(oAppConst.AccessPolicies.LevelTwo)] /// Ready For Test public async Task <IActionResult> Put([FromBody] oCategory modifiedCategory) { try { /// get the current category object without tracking it oCategory currentCatogory = await DbContext.Categories .AsNoTracking() .SingleOrDefaultAsync(c => c.Id == modifiedCategory.Id) .ConfigureAwait(false); // if the current category does not exists if (currentCatogory == null) { oAppFunc.Error(ref ErrorsList, "Store Not Found"); return(NotFound(ErrorsList)); } TryValidateModel(modifiedCategory); ModelState.Remove("ImageBase64"); /// if model validation failed if (!ModelState.IsValid) { oAppFunc.ExtractErrors(ModelState, ref ErrorsList); /// return Unprocessable Entity with all the errors return(UnprocessableEntity(ErrorsList)); } /// check the database to see if a Category with the same name exists if (await DbContext.Categories .AsNoTracking() .AnyAsync(c => c.Name == modifiedCategory.Name && c.Id != modifiedCategory.Id) .ConfigureAwait(false)) { /// extract the errors and return bad request containing the errors oAppFunc.Error(ref ErrorsList, "Category already exists."); return(StatusCode(412, ErrorsList)); } if (!string.IsNullOrWhiteSpace(modifiedCategory.ImageBase64)) { try { string oldImagePath = modifiedCategory.ImagePath.Clone().ToString(); modifiedCategory.ImagePath = oAppFunc.SaveImageToWWWRoot(modifiedCategory.Name, WebHost.WebRootPath, modifiedCategory.ImageBase64, @"Images/Categories"); oAppFunc.DeleteImage(oldImagePath, WebHost.WebRootPath); } catch (Exception) { oAppFunc.Error(ref ErrorsList, "Image cannot be saved."); return(StatusCode(412, ErrorsList)); } } /// else Category object is made without any errors /// Update the current Category to the EF context DbContext.Categories.Update(modifiedCategory); /// save the changes to the database await DbContext.SaveChangesAsync().ConfigureAwait(false); /// return 200 OK (Update) status with the modified object /// and success message return(Ok(modifiedCategory)); } catch (Exception) // DbUpdateException, DbUpdateConcurrencyException { /// Add the error below to the error list and return bad request oAppFunc.Error(ref ErrorsList, oAppConst.CommonErrors.ServerError); return(StatusCode(417, ErrorsList)); } }