public async Task <IActionResult> Create([FromBody] RecordInputModel model) { var userId = User.GetId(); await ValidateCategoryExists(userId, model.CategoryId); var record = new Record { Type = model.Type, Name = model.Name, Amount = model.Amount, CategoryId = model.CategoryId, Date = model.Date, ApplicationUserId = userId }; _context.Add(record); await _context.SaveChangesAsync(); return(Ok( new RecordOutputModel { Type = record.Type, Name = record.Name, Amount = record.Amount, Id = record.Id, CategoryId = record.CategoryId, Date = record.Date } )); }
public async Task <IActionResult> Edit(int id, [FromBody] RecordInputModel model) { var userId = User.GetId(); await ValidateCategoryExists(userId, model.CategoryId); var record = await _context.Record .Where(x => x.ApplicationUserId == userId || x.ApplicationUser.UserGroup.ApplicationUsers.Any(user => user.Id == userId)) .SingleOrDefaultAsync(x => x.Id == id); if (record == null) { throw new ArgumentException("Record not found"); } record.Name = model.Name; record.Amount = model.Amount; record.Type = model.Type; record.Date = model.Date; record.CategoryId = model.CategoryId; _context.Update(record); _context.SaveChanges(); return(Ok()); }