public async Task UpdateNotEnoughMoneyTwo() { await context.AddAsync(new Record { UserId = userId, CategoryId = categoryId, Amount = 9000, Description = "", IsIncome = false }); var user = await context.Users.FindAsync(userId); user.Bill = 1000; await context.SaveChangesAsync(); var amount = 8000; var description = "Update"; var isIncome = true; var model = new UpdateRecordModel { Id = recordId, Amount = amount, Description = description, IsIncome = isIncome, CategoryId = categoryId }; var result = await recordService.Update(userId, model); Assert.IsFalse(result.Succeeded); Assert.AreEqual("Not enough money", result.Error.Message); }
public async Task <ServiceResponse <RecordModel> > Update(string userId, UpdateRecordModel model) { var record = await context.Records.FindAsync(model.Id); if (record == null) { return(Error("Record not Found")); } var category = await context.Categories.FindAsync(model.CategoryId); if (category == null) { return(Error("Category not Found")); } if (category.UserId != userId || record.UserId != userId) { return(Error("Access Forbidden")); } var user = await context.Users.FindAsync(userId); if (user == null) { return(Error("User not Found")); } if (record.IsIncome) { user.Bill -= record.Amount; } else { user.Bill += record.Amount; } if (model.IsIncome) { user.Bill += model.Amount; } else { user.Bill -= model.Amount; } if (user.Bill < 0) { return(Error("Not enough money")); } mapper.Map(model, record); record.UserId = userId; try { await context.SaveChangesAsync(); } catch { return(Error("Update Failed")); } return(Success(mapper.Map <Record, RecordModel>(record))); }
public async Task <IActionResult> Put([FromBody] UpdateRecordModel model) { var result = await recordService.Update(UserId, model); if (result.Succeeded) { return(Ok(result.Response)); } return(BadRequest(result.Error)); }
public async Task UpdateNotEnoughMoneyOne() { var amount = 10000; var description = "Update"; var isIncome = false; var model = new UpdateRecordModel { Id = recordId, Amount = amount, Description = description, IsIncome = isIncome, CategoryId = categoryId }; var result = await recordService.Update(userId, model); Assert.IsFalse(result.Succeeded); Assert.AreEqual("Not enough money", result.Error.Message); }
public async Task UpdateWrongId() { var amount = 11000; var description = "Update"; var isIncome = true; var model = new UpdateRecordModel { Id = wrongRecordId, Amount = amount, Description = description, IsIncome = isIncome, CategoryId = categoryId }; var result = await recordService.Update(userId, model); Assert.IsFalse(result.Succeeded); Assert.AreEqual("Record not Found", result.Error.Message); }
public async Task <int> UpdateAsync(UpdateRecordModel updateRecordModel) { var record = await _recordRepository.GetRecordByIdAsync(updateRecordModel.Id); if (record == null) { throw new BudgetValidationException( string.Format(ValidationMessages.Common.EntityDoesNotExist, nameof(record), updateRecordModel.Id)); } record.AccountId = updateRecordModel.AccountId; record.Amount = updateRecordModel.Amount; record.Note = updateRecordModel.Note; record.CategoryId = updateRecordModel.CategoryId; record.PaymentTypeId = updateRecordModel.PaymentTypeId; record.RecordType = updateRecordModel.RecordType; var updatedRecord = await _recordRepository.UpdateAsync(record); return(updatedRecord.Id); }
public async Task Update() { var amount = 11000; var description = "Update"; var isIncome = true; var model = new UpdateRecordModel { Id = recordId, Amount = amount, Description = description, IsIncome = isIncome, CategoryId = categoryId }; var result = await recordService.Update(userId, model); Assert.IsTrue(result.Succeeded); Assert.AreEqual(amount, result.Response.Amount); Assert.AreEqual(description, result.Response.Description); Assert.AreEqual(isIncome, result.Response.IsIncome); Assert.AreEqual(recordId, result.Response.Id); }
public async Task <IActionResult> UpdateRecord(int id, [FromBody] JsonPatchDocument <UpdateRecordModel> model) { CustomValidation(); var userId = User.GetSubjectId(); var user = await _databaseContext.UserData.FirstOrDefaultAsync(UserIdPredicate(userId)); if (user == null) { throw new HttpResponseException(null, HttpStatusCode.Unauthorized); } var recordToUpdate = await _databaseContext.Record .Include(r => r.Image) .Include(r => r.RecordType) .Include(r => r.RecordFormat) .FirstOrDefaultAsync(r => r.Id == id && r.OwnerId == user.Id); var recordStyles = _databaseContext.RecordStyle .Include(rs => rs.Style) .Where(r => r.RecordId == id); if (recordToUpdate == null) { throw new HttpResponseException(null, HttpStatusCode.NotFound); } var newModel = new UpdateRecordModel { Artist = recordToUpdate.Artist, Name = recordToUpdate.Name, Description = recordToUpdate.Description, Label = recordToUpdate.Label, Year = recordToUpdate.Year, RecordTypeId = recordToUpdate.RecordTypeId, RecordFormatId = recordToUpdate.RecordFormatId, ImageId = recordToUpdate.ImageId, StyleIds = recordStyles.Select(rs => rs.StyleId).ToArray(), Rating = recordToUpdate.Rating, RecordLength = recordToUpdate.RecordLength }; model.ApplyTo(newModel, ModelState); CustomValidation(newModel); if (newModel.ImageId != null) { var image = await _databaseContext.Image.FirstOrDefaultAsync(i => i.Id == newModel.ImageId && i.CreatorId == user.Id); if (image == null) { throw new HttpResponseException(null, HttpStatusCode.Forbidden); } } var newRecordType = await _databaseContext.RecordType .FirstOrDefaultAsync(rt => rt.Id == newModel.RecordTypeId); var newRecordFormat = await _databaseContext.RecordFormat .FirstOrDefaultAsync(rt => rt.Id == newModel.RecordFormatId); var newStyles = _databaseContext.Style .Where(s => newModel.StyleIds.Contains(s.Id)); var firstNewStyle = await newStyles.FirstOrDefaultAsync(); if (newRecordType == null || newStyles.Count() != newModel.StyleIds.Length || firstNewStyle == null || !newStyles.All(s => s.GenreId == firstNewStyle.GenreId) || newRecordFormat == null) { throw new HttpResponseException(null, HttpStatusCode.BadRequest); } //TODO: Avoid dirty update _databaseContext.RecordStyle.RemoveRange(recordStyles); var newRecordStyles = newStyles.Select(s => new RecordStyle { RecordId = recordToUpdate.Id, StyleId = s.Id }); await _databaseContext.RecordStyle.AddRangeAsync(newRecordStyles); //TODO: Automapper recordToUpdate.Artist = newModel.Artist; recordToUpdate.Name = newModel.Name; recordToUpdate.Description = newModel.Description; recordToUpdate.Label = newModel.Label; recordToUpdate.Year = newModel.Year; recordToUpdate.Rating = newModel.Rating; recordToUpdate.RecordLength = newModel.RecordLength; recordToUpdate.ImageId = newModel.ImageId; recordToUpdate.RecordTypeId = newRecordType.Id; recordToUpdate.RecordFormatId = newRecordFormat.Id; await _databaseContext.SaveChangesAsync(); await _userActivityService.CreateActivityAsync(UserActivityActionName.RecordUpdate, user.Id, record : recordToUpdate); return(Ok(recordToUpdate)); }
public async Task <IActionResult> Update(UpdateRecordModel updateRecordModel) => Ok(await _recordService.UpdateAsync(updateRecordModel));