public async Task CreateOrUpdate(Fidget fidget, string userName, bool canApproveFidgets) { var existing = await _context.Fidgets.FindAsync(fidget.Id); if (existing == null) { await _context.Fidgets.AddAsync(fidget); } else { var findType = await _context.FidgetTypes.FindAsync(fidget.TypeId); existing.Color = fidget.Color; existing.Name = fidget.Name; if (canApproveFidgets && !existing.IsApproved && fidget.IsApproved) { existing.IsApproved = fidget.IsApproved; existing.ApprovedBy = userName; } existing.Type = findType; // better, but i don't like this } await _context.SaveChangesAsync(true); }
public async Task <IActionResult> CreateOrUpdateFidget(Fidget fidget) { if (User == null) { return(Unauthorized()); } await _fidgetRepository.CreateOrUpdate(fidget, AuthenticatedUserName, CanApproveFidgets); return(Ok()); }