Beispiel #1
0
        private async Task <int> PutReviewAsync(Review reviewToUpdate, Guid?userId = null)
        {
            context.Entry(reviewToUpdate).State = EntityState.Modified;
            var dbReview = await context.Reviews.Include(r => r.Records).Include(r => r.User).AsNoTracking().Where(r => r.Guid == reviewToUpdate.Guid).FirstOrDefaultAsync();

            if (dbReview.User.Guid != userId)
            {
                throw new Exception("Cannot update review which does not belong to the user");
            }

            foreach (var record in dbReview.Records)
            {
                var recordToUdpate = reviewToUpdate.Records.SingleOrDefault(e => e.Guid == record.Guid);
                if (recordToUdpate != null)
                {
                    record.Update(recordToUdpate);
                    context.Entry(record).State = EntityState.Modified;
                }
                else
                {
                    context.Entry(record).State = EntityState.Deleted;
                }
            }
            foreach (var record in reviewToUpdate.Records.Where(e => !dbReview.Records.Any(dbE => dbE.Guid == e.Guid)))
            {
                context.Entry(record).State = EntityState.Added;
            }
            dbReview.Update(reviewToUpdate);
            return(await context.SaveChangesAsync());
        }
 private async Task <int> PutUserAsync(User userToUpdate)
 {
     context.Entry(userToUpdate).State = EntityState.Modified;
     return(await context.SaveChangesAsync());
 }