public async Task <bool> Handle( Command request, CancellationToken cancellationToken ) { Guid id = request.UserInterestId; UserInterest existingUserInterest = _interestsDbContext.UserInterests.Find(id); if (existingUserInterest != null) { Guid interestId = existingUserInterest.IdInterest; _interestsDbContext.UserInterests.Remove(existingUserInterest); Interest existingInterest = _interestsDbContext.Interests.Find(interestId); if (existingInterest != null) { existingInterest.PeopleCount = existingInterest.PeopleCount - 1; _interestsDbContext.Interests.Update(existingInterest); return(await _interestsDbContext.SaveChangesAsync() > 0); } else { throw new AppException($"The interest {interestId} already as not been found"); } } else { throw new AppException($"The user interest {id} already as not been found"); } }
public async Task <bool> Handle( Command request, CancellationToken cancellationToken ) { Guid id = request.Id; int visibility = request.Visibility; Guid userId = request.UserId; Interest existingInterest = _interestsDbContext.Interests.Find(id); if (existingInterest != null) { UserInterest existingUserInterest = await _interestsDbContext.UserInterests.AsNoTracking().SingleOrDefaultAsync(u => u.IdInterest == id && u.IdUser == userId); if (existingUserInterest != null) { throw new AppException($"The user interest {id} has already been added"); } else { UserInterest userInterest = new UserInterest { IdInterest = id, Visibility = visibility, IdUser = userId }; _interestsDbContext.UserInterests.Add(userInterest); existingInterest.PeopleCount = existingInterest.PeopleCount + 1; _interestsDbContext.Interests.Update(existingInterest); return(await _interestsDbContext.SaveChangesAsync() > 0); } } else { throw new AppException($"The interest {id} already as not been found"); } }
public async Task <bool> Handle( Command request, CancellationToken cancellationToken ) { string name = request.Name; int visibility = request.Visibility; Guid userId = request.UserId; Interest existingInterest = await _interestsDbContext.Interests.AsNoTracking().SingleOrDefaultAsync(i => i.Name == name); if (existingInterest != null) { throw new AppException($"The interest {name} has already been added"); } else { Guid interestId = Guid.NewGuid(); Interest Interest = new Interest { Id = interestId, Name = name, PeopleCount = 1, Visibility = visibility }; _interestsDbContext.Interests.Add(Interest); UserInterest userInterest = new UserInterest { IdInterest = interestId, Visibility = visibility, IdUser = userId }; _interestsDbContext.UserInterests.Add(userInterest); return(await _interestsDbContext.SaveChangesAsync() > 0); } }