public async Task <ActionResult <EditSaleResourceModel> > Update(EditSaleInputModel inputModel) { try { var saleInfoModel = await this.GetSaleInfo(inputModel.SaleId); if (saleInfoModel == null) { return(this.NotFound()); } var currentUserId = this.GetUserId(this.User); if (saleInfoModel.SellerId == currentUserId && saleInfoModel.Status == Status.Open) { var saleModel = await this.salesService.EditSale <EditSaleResourceModel>(inputModel); await this.saleLogsService.ClearSaleLogs(saleInfoModel.Id); await this.saleLogHubContext.Clients.Group(saleModel.Id.ToString()) .RecieveLogNotification("Sale was edited by seller!"); return(saleModel); } return(this.Forbid()); } catch (Exception ex) { this.loggerService.LogException(ex); return(this.BadRequest()); } }
public async Task <TModel> EditSale <TModel>(EditSaleInputModel inputModel) { var sale = await this.GetSale(inputModel.SaleId); var address = await this.addressesEntityRetriever.GetAddress(inputModel.ShipsFromAddressId); if (sale == null) { throw new NullReferenceException(SaleNotFound); } if (address == null) { throw new NullReferenceException(AddressNotFound); } sale.Price = inputModel.Price; sale.SleeveGrade = inputModel.SleeveGrade; sale.VinylGrade = inputModel.VinylGrade; sale.Description = inputModel.Description; sale.ShipsFrom = $"{address.Country} - {address.Town}"; sale.Status = Status.Open; sale.ModifiedOn = DateTime.UtcNow; await this.dbContext.SaveChangesAsync(); return(sale.To <TModel>()); }
public async Task EditSaleShouldThrowNullRefferenceExceptionIfProvidedSaleIdIsNotInDb() { this.addressesEntityRetrieverMock.Setup(x => x.GetAddress(It.IsAny <Guid?>())).ReturnsAsync(new Address()); var editSaleInputModel = new EditSaleInputModel(); var exception = await Assert.ThrowsAsync <NullReferenceException>( async() => await this.salesService.EditSale <EditSaleResourceModel>( editSaleInputModel)); Assert.Equal(SaleNotFound, exception.Message); }
public async Task EditSaleShouldEditSaleWithCorrectData() { var release = new Release(); var user = new VinylExchangeUser(); var updatedAddress = this.testAddress; this.releasesEntityRetrieverMock.Setup(x => x.GetRelease(It.IsAny <Guid?>())).ReturnsAsync(release); this.usersEntityRetrieverMock.Setup(x => x.GetUser(It.IsAny <Guid?>())).ReturnsAsync(user); this.addressesEntityRetrieverMock.Setup(x => x.GetAddress(It.IsAny <Guid?>())).ReturnsAsync(updatedAddress); var addressProperties = new List <string> { updatedAddress.Country, updatedAddress.Town }; var sale = new Sale { VinylGrade = Condition.Poor, SleeveGrade = Condition.NearMint, Description = "blbbebe", Price = 50, ShipsFrom = "Paris France" }; await this.dbContext.Sales.AddAsync(sale); await this.dbContext.SaveChangesAsync(); var editSaleInputModel = new EditSaleInputModel { VinylGrade = Condition.Mint, SleeveGrade = Condition.Mint, Description = "updated description", Price = 100, SaleId = sale.Id }; await this.salesService.EditSale <EditSaleResourceModel>(editSaleInputModel); var updatedSale = await this.dbContext.Sales.FirstOrDefaultAsync(s => s.Id == sale.Id); Assert.Equal(editSaleInputModel.SaleId, updatedSale.Id); Assert.Equal(editSaleInputModel.VinylGrade, updatedSale.VinylGrade); Assert.Equal(editSaleInputModel.SleeveGrade, updatedSale.SleeveGrade); Assert.Equal(editSaleInputModel.Description, updatedSale.Description); Assert.Equal(editSaleInputModel.Price, updatedSale.Price); Assert.True(addressProperties.Select(ap => updatedSale.ShipsFrom.Contains(ap)).All(x => x)); }
public async Task <IActionResult> Edit(int id, EditSaleInputModel input) { var user = await this.userManager.GetUserAsync(this.User); input.CountryId = user.CountryId; if (!this.ModelState.IsValid) { return(this.View()); } await this.salesService.UpdateSaleAsync(id, input); return(this.RedirectToAction(nameof(this.SaleInfo), new { id })); }
public async Task EditSaleShouldThrowNullRefferenceExceptionIfProvidedAddressIdIsNotInDb() { var sale = new Sale(); await this.dbContext.Sales.AddAsync(sale); await this.dbContext.SaveChangesAsync(); this.addressesEntityRetrieverMock.Setup(x => x.GetAddress(It.IsAny <Guid?>())).ReturnsAsync((Address)null); var editSaleInputModel = new EditSaleInputModel { SaleId = sale.Id, ShipsFromAddressId = Guid.NewGuid() }; var exception = await Assert.ThrowsAsync <NullReferenceException>( async() => await this.salesService.EditSale <EditSaleResourceModel>( editSaleInputModel)); Assert.Equal(AddressNotFound, exception.Message); }
public async Task <SaleViewModel> UpdateSaleAsync(int id, EditSaleInputModel input) { var sale = this.salesRepository.All() .Include(x => x.Car) .FirstOrDefault(x => x.Id == id); sale.CountryId = input.CountryId; sale.CityId = input.CityId; sale.CountryId = input.CountryId; sale.DaysValid = input.DaysValid; sale.Description = input.Description; sale.ModifiedOn = DateTime.UtcNow; sale.Price = input.Price; await this.salesRepository.SaveChangesAsync(); await this.carsService.UpdateCarAsync(sale.Car.Id, input.Car); return(await this.GetSingleSaleInfo(id)); }