public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { //get user var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUserName()); //get photo var photo = user.Photos.FirstOrDefault(x => x.Id == request.Id); //ensure photo exists (also caught if photo not owned by user) if (photo == null) { throw new Application.Errors.RestException(HttpStatusCode.NotFound, new { photo = "Photo not found." }); } //get main photo var mainPhoto = user.Photos.FirstOrDefault(x => x.IsMain); //change main mainPhoto.IsMain = false; photo.IsMain = true; var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem saving changes."); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { //get user and target var observer = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUserName()); var target = await _context.Users.SingleOrDefaultAsync(x => x.UserName == request.UserName); if (target == null) { throw new Application.Errors.RestException(HttpStatusCode.NotFound, new { user = "******" }); } //attempt to find var following = await _context.Followings.SingleOrDefaultAsync(x => x.TargetId == target.Id && x.ObserverId == observer.Id); if (following == null) { throw new Application.Errors.RestException(HttpStatusCode.BadRequest, new { following = "You are not following this user." }); } //delete following _context.Followings.Remove(following); var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem saving changes."); }
public async Task <Domain.Photo> Handle(Command request, CancellationToken cancellationToken) { //upload photo (errors caught by PhotoAccessor) var photoUploadResult = _photoAccessor.AddPhoto(request.File); //get user var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUserName()); //create photo var photo = new Domain.Photo { Id = photoUploadResult.PublicId, Url = photoUploadResult.Url }; //if user has no main photos, use this one if (!user.Photos.Any(x => x.IsMain)) { photo.IsMain = true; } //save to database - updates user and adds photo user.Photos.Add(photo); var success = await _context.SaveChangesAsync() > 0; if (success) { return(photo); } throw new Exception("Problem saving changes."); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { var activity = await _context.Activities.FindAsync(request.Id); //throw custom exception if activity is not found if (activity == null) { throw new Application.Errors.RestException(HttpStatusCode.NotFound, new { activity = "Activity not found." }); } //?? is null coalescing operator activity.Title = request.Title ?? activity.Title; activity.Description = request.Description ?? activity.Description; activity.Category = request.Category ?? activity.Category; //Added ? at end of date above so it can be optional, otherwise causes error because date cannot be null activity.Date = request.Date ?? activity.Date; activity.City = request.City ?? activity.City; activity.Venue = request.Venue ?? activity.Venue; var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem saving changes."); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { var activity = await _context.Activities.FindAsync(request.Id); _context.Remove(activity); await _context.SaveChangesAsync(); return(Unit.Value); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { //get user var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUserName()); //update user user.DisplayName = request.DisplayName ?? user.DisplayName; user.Bio = request.Bio ?? user.Bio; var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new System.Exception("Problem saving changes."); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { //get activity and ensure not null var activity = await _context.Activities.FindAsync(request.Id); if (activity == null) { throw new Application.Errors.RestException(HttpStatusCode.NotFound, new { activity = "Activity not found." }); } //get user - can't be null because token is required var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUserName()); //see if attendance already exists var attendance = await _context.UserActivities .SingleOrDefaultAsync(x => x.ActivityId == activity.Id && x.AppUserId == user.Id); if (attendance != null) { throw new Application.Errors.RestException(HttpStatusCode.BadRequest, new { attendance = "User already attending activity." }); } //create attendance attendance = new Domain.UserActivity { Activity = activity, AppUser = user, DateJoined = DateTime.Now, IsHost = false }; _context.UserActivities.Add(attendance); var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem saving changes."); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { //get user var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUserName()); //get photo from user photos var photo = user.Photos.FirstOrDefault(x => x.Id == request.Id); //ensure photo exists (also caught if photo not owned by user) if (photo == null) { throw new Application.Errors.RestException(HttpStatusCode.NotFound, new { photo = "Photo not found." }); } //don't delete photo if it is the user's main photo if (photo.IsMain) { throw new Application.Errors.RestException(HttpStatusCode.BadRequest, new { photo = "You cannot delete your main photo." }); } //delete photo on Cloudinary var result = _photoAccessor.DeletePhoto(photo.Id); //ensure deletion worked if (result == null) { throw new Exception("Problem deleting the photo."); } //delete photo in database user.Photos.Remove(photo); var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem saving changes."); }
//return Unit, not anything to do with activity public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { //compose activity var activity = new Domain.Activity { Id = request.Id, Title = request.Title, Description = request.Description, Category = request.Category, Date = request.Date, City = request.City, Venue = request.Venue }; //add activity to DB _context.Add(activity); //get user from DB var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUserName()); //create attendee - notice AppUserId and ActivityId are not required var attendee = new Domain.UserActivity { AppUser = user, Activity = activity, DateJoined = DateTime.Now, IsHost = true }; //add attendee to database _context.UserActivities.Add(attendee); //SaveChangesAsync returns and int, so we check to make sure that at least one change has occured var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem saving changes."); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { var activity = await _context.Activities.FindAsync(request.Id); //throw custom exception if activity is not found if (activity == null) { throw new Application.Errors.RestException(HttpStatusCode.NotFound, new { activity = "Activity not found." }); } _context.Remove(activity); var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem saving changes."); }
public async Task <CommentDTO> Handle(Command request, CancellationToken cancellationToken) { //get activity var activity = await _context.Activities.FindAsync(request.ActivityId); if (activity == null) { throw new Application.Errors.RestException(HttpStatusCode.NotFound, new { activity = "Activity not found." }); } //get user var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == request.UserName); if (user == null) { throw new Application.Errors.RestException(HttpStatusCode.NotFound, new { user = "******" }); } //create comment var comment = new Domain.Comment { Author = user, Body = request.Body, CreatedAt = DateTime.Now, Activity = activity }; //add comment to DB activity.Comments.Add(comment); var success = await _context.SaveChangesAsync() > 0; //map and return DTO if (success) { return(_mapper.Map <Domain.Comment, CommentDTO>(comment)); } throw new Exception("Problem saving changes."); }