public async Task <ActionResult <PlantDetailsViewModel> > CreatePlantDetails([FromForm] CreateEditPlantDetailsViewModel plantDetailsViewModel) { var plantDetails = new PlantDetails(); GetChangesFromViewModel(plantDetails, plantDetailsViewModel); plantDetails.UserId = plantDetailsViewModel.UserId; if (!plantDetails.IsValid()) { return(BadRequest()); } if ((plantDetailsViewModel.ImageFile != null && !plantDetailsViewModel.ImageFile.IsValidImage())) { return(BadRequest(new { Message = $"Invalid image" })); } if (plantDetailsViewModel.ImageFile != null) { var plantImageViewModel = await _plantImageService.UploadImageAsync(plantDetailsViewModel.ImageFile); plantDetails.ImageName = plantImageViewModel.ImageName; } _context.PlantDetails.Add(plantDetails); await _context.SaveChangesAsync(); return(CreatedAtAction("GetPlantDetails", new { id = plantDetails.PlantDetailsId }, MapToViewModel(plantDetails))); }
[InlineData("cktan", "Fragrant Garlic", "Fragrant Garlic (Small)", "Spice", 1.50, 100.0, "g", -1)] // stock public async Task Create_plant_details_bad_request_reponse(string userId, string name, string description, string category, decimal price, decimal weight, string unit, int stock) { //Arrange var plantDetailsContext = new PlantListingContext(_dbOptions); var invalidCreatePlantDetailsViewModel = new CreateEditPlantDetailsViewModel() { PlantDetailsId = 0, Name = name, Description = description, Category = category, Price = price, Weight = weight, Unit = unit, Stock = stock, UserId = userId }; //Act var plantDetailsController = new PlantListingController(plantDetailsContext, _mockPlantImageService.Object); var actionResult = await plantDetailsController.CreatePlantDetails(invalidCreatePlantDetailsViewModel); //Assert Assert.IsType <BadRequestResult>(actionResult.Result); }
public async Task Update_plant_details_success() { //Arrange var plantDetailsContext = new PlantListingContext(_dbOptions); var userId = "wpkeoh"; var updatePlantDetailsViewModel = new CreateEditPlantDetailsViewModel() { PlantDetailsId = 5, Name = "Fragrant Garlic", Description = "Fragrant Garlic (Small)", Category = "Spice", Price = 1.50m, Weight = 100.0m, Unit = "g", Stock = 100, UserId = userId }; //Act var plantDetailsController = new PlantListingController(plantDetailsContext, _mockPlantImageService.Object); var actionResult = await plantDetailsController.UpdatePlantDetails(updatePlantDetailsViewModel); //Assert Assert.IsType <NoContentResult>(actionResult); }
public async Task Update_plant_details_not_found_reponse() { //Arrange var plantDetailsContext = new PlantListingContext(_dbOptions); var userId = "cktan"; var NotFoundUpdatePlantDetailsViewModel = new CreateEditPlantDetailsViewModel() { PlantDetailsId = 99, Name = "Bean sprouts", Description = "Organic bean sprouts", Category = "Vegetable", Price = 0.80m, Weight = 200.0m, Unit = "g", Stock = 100, UserId = userId }; //Act var plantDetailsController = new PlantListingController(plantDetailsContext, _mockPlantImageService.Object); var actionResult = await plantDetailsController.UpdatePlantDetails(NotFoundUpdatePlantDetailsViewModel); //Assert Assert.IsType <NotFoundObjectResult>(actionResult); }
public async Task <IActionResult> UpdatePlantDetails([FromForm] CreateEditPlantDetailsViewModel plantDetailsViewModel) { var plantDetails = await _context.PlantDetails.FindAsync(plantDetailsViewModel.PlantDetailsId); if (plantDetails == null) { return(NotFound(new { Message = $"Plant details with id {plantDetailsViewModel.PlantDetailsId} not found." })); } if (plantDetails.UserId != plantDetailsViewModel.UserId) { return(Unauthorized()); } GetChangesFromViewModel(plantDetails, plantDetailsViewModel); if (!plantDetails.IsValid()) { return(BadRequest()); } if ((plantDetailsViewModel.ImageFile != null && !plantDetailsViewModel.ImageFile.IsValidImage())) { return(BadRequest(new { Message = $"Invalid image" })); } try { if (plantDetailsViewModel.ImageFile != null) { var currentImageName = plantDetails.ImageName; var plantImageViewModel = await _plantImageService.ReplaceImageAsync(currentImageName, plantDetailsViewModel.ImageFile); plantDetails.ImageName = plantImageViewModel.ImageName; } _context.Entry(plantDetails).State = EntityState.Modified; await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PlantDetailsExists(plantDetailsViewModel.PlantDetailsId)) { return(NotFound(new { Message = $"Plant details with id {plantDetailsViewModel.PlantDetailsId} not found." })); } else { throw; } } return(NoContent()); }
private void GetChangesFromViewModel(PlantDetails plantDetails, CreateEditPlantDetailsViewModel viewModel) { var category = _context.PlantCategories.Where(c => EF.Functions.Like(c.Category, viewModel.Category ?? string.Empty)).FirstOrDefault(); var weightUnit = _context.WeightUnits.Where(c => EF.Functions.Like(c.Unit, viewModel.Unit ?? string.Empty)).FirstOrDefault(); plantDetails.Name = viewModel.Name; plantDetails.Description = viewModel.Description; plantDetails.CategoryId = category?.Id ?? -1; plantDetails.Price = viewModel.Price; plantDetails.Weight = viewModel.Weight; plantDetails.UnitId = weightUnit?.Id ?? -1; plantDetails.Stock = viewModel.Stock; }
public async Task Create_plant_details_success() { //Arrange var plantDetailsContext = new PlantListingContext(_dbOptions); var userId = "cktan"; var validCreatePlantDetailsViewModel = new CreateEditPlantDetailsViewModel() { Name = "Bean sprouts", Description = "Organic bean sprouts", Category = "Vegetable", Price = 0.80m, Weight = 100.0m, Unit = "g", Stock = 100, UserId = userId }; var expectedPlantDetailsId = 9; var expectedName = "Bean sprouts"; var expectedDescription = "Organic bean sprouts"; var expectedCategory = "Vegetable"; var expectedPrice = 0.80m; var expectedWeight = 100.0m; var expectedUnit = "g"; var expectedStock = 100; var expectedUserId = "cktan"; //Act var plantDetailsController = new PlantListingController(plantDetailsContext, _mockPlantImageService.Object); var actionResult = await plantDetailsController.CreatePlantDetails(validCreatePlantDetailsViewModel); //Assert Assert.IsType <CreatedAtActionResult>(actionResult.Result); Assert.Equal("GetPlantDetails", (actionResult.Result as CreatedAtActionResult).ActionName); Assert.IsType <PlantDetailsViewModel>((actionResult.Result as CreatedAtActionResult).Value); PlantDetailsViewModel actualViewModel = (actionResult.Result as CreatedAtActionResult).Value as PlantDetailsViewModel; Assert.Equal(expectedPlantDetailsId, actualViewModel.PlantDetailsId); Assert.Equal(expectedName, actualViewModel.Name); Assert.Equal(expectedDescription, actualViewModel.Description); Assert.Equal(expectedCategory, actualViewModel.Category); Assert.Equal(expectedPrice, actualViewModel.Price); Assert.Equal(expectedWeight, actualViewModel.Weight); Assert.Equal(expectedUnit, actualViewModel.Unit); Assert.Equal(expectedStock, actualViewModel.Stock); Assert.Equal(expectedUserId, actualViewModel.UserId); }