public async Task <DatumModel> UpdateAsync(string stuffId, DatumModel input) { input.CheckDatum(); if (stuffId != input.Id) { throw new ArgumentException("Corrupted data."); } TUser dbUserAuth = await _userAuth.GetCurrentUserAsync("updated datum"); TStuff dbStuff = await _context.TStuff.FirstOrDefaultAsync(x => x.StfId.Equals(stuffId)); if (dbStuff == null || dbStuff.StfUserId != dbUserAuth.UsrId) { throw new ArgumentException("Corrupted data."); } // Update stuff dbStuff = input.ToUpdate(dbStuff); await _context.SaveChangesAsync(); // Attach user to the stuff for the response dbStuff.StfUser = dbUserAuth; var result = dbStuff.ToDatumModel(); return(result); }
public async Task <DatumModel> CreateAsync(DatumModel input) { input.CheckDatum(); TStuff dbStuff = input.ToCreate(); TUser dbUserAuth = await _userAuth.GetCurrentUserAsync("created datum"); TUser dbUser = await _context.TUser.FirstOrDefaultAsync(x => x.UsrId.Equals(dbUserAuth.UsrId)); if (dbUser == null) { // Create and attach new user dbUserAuth.UsrCreatedAt = DateTime.UtcNow.ToStrDate(); dbStuff.StfUser = dbUserAuth; } // Attach foreign key dbStuff.StfUserId = dbUserAuth.UsrId; // Create stuff await _context.TStuff.AddAsync(dbStuff); await _context.SaveChangesAsync(); // Attach user to the stuff for the response dbStuff.StfUser = dbUserAuth; var result = dbStuff.ToDatumModel(); return(result); }
public async Task <DatumModel> ReadAsync(string stuffId) { // Get the stuff and its user TStuff dbStuff = await _context.TStuff .Where(x => x.StfId.Equals(stuffId)) .Include(x => x.StfUser) .FirstOrDefaultAsync(); if (dbStuff == null) { throw new NotFoundException("Stuff not found."); } var result = dbStuff.ToDatumModel(); return(result); }