Exemplo n.º 1
0
            public async Task <Result <CommentDto> > Handle(Command request, CancellationToken cancellationToken)
            {
                var activity = await _context.Activities.FindAsync(request.ActivityId);

                if (activity == null)
                {
                    return(null);
                }

                var user = await _context.Users
                           .Include(p => p.Photos)
                           .SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetUsername());

                var comment = new Comment
                {
                    Author   = user,
                    Activity = activity,
                    Body     = request.Body
                };

                activity.Comments.Add(comment);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Result <CommentDto> .Success(_mapper.Map <CommentDto>(comment)));
                }

                return(Result <CommentDto> .Failure("Failed to add comment"));
            }
Exemplo n.º 2
0
        public async Task <Unit> Handle(DeleteFollowerCommand request, CancellationToken cancellationToken)
        {
            var observer =
                await _context.Users.SingleOrDefaultAsync(u => u.UserName == _userAccessor.GetCurrentUsername(),
                                                          cancellationToken);

            var target = await _context.Users.SingleOrDefaultAsync(u => u.UserName == request.Username, cancellationToken);

            if (target == null)
            {
                throw new RestException(HttpStatusCode.NotFound, new { User = "******" });
            }

            var following =
                await _context.Followings.SingleOrDefaultAsync(f =>
                                                               f.ObserverId == observer.Id && f.TargetId == target.Id, cancellationToken);

            if (following == null)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
            }

            _context.Followings.Remove(following);

            var success = await _context.SaveChangesAsync(cancellationToken) > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Problem saving changes.");
        }
Exemplo n.º 3
0
            public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.FirstOrDefaultAsync(x
                                                                    => x.UserName == _userAccessor.GetUsername());

                var attendee = new ActivityAttendee
                {
                    AppUser  = user,
                    Activity = request.Activity,
                    IsHost   = true
                };

                request.Activity.Attendees.Add(attendee);

                _context.Activities.Add(request.Activity);

                var result = await _context.SaveChangesAsync() > 0;

                if (!result)
                {
                    Result <Unit> .Failure("Failed to create activity");
                }

                return(Result <Unit> .Success(Unit.Value));
            }
Exemplo n.º 4
0
        public async Task <Unit> Handle(UpdateActivityCommand request, CancellationToken cancellationToken)
        {
            var activity = await _context.Activities.FindAsync(request.Id);

            if (activity == null)
            {
                throw new RestException(HttpStatusCode.NotFound, "Could not find activity.");
            }

            activity.Name        = request.Name ?? activity.Name;
            activity.Description = request.Description ?? activity.Description;
            activity.Category    = request.Category ?? activity.Category;
            activity.Date        = request.Date ?? activity.Date;
            activity.City        = request.City ?? activity.City;
            activity.Venue       = request.Venue ?? activity.Venue;

            var success = await _context.SaveChangesAsync(cancellationToken) > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Error during save changes.");
        }
        public async Task <CommentDto> Handle(CreateCommentCommand request, CancellationToken cancellationToken)
        {
            var activity = await _context.Activities.FindAsync(request.ActivityId);

            if (activity == null)
            {
                throw new RestException(HttpStatusCode.NotFound, new { Activity = "Could not find activity." });
            }

            var user = await _context.Users.SingleOrDefaultAsync(u => u.UserName == request.Username,
                                                                 cancellationToken);

            var comment = new Comment
            {
                Author    = user,
                Activity  = activity,
                Body      = request.Body,
                CreatedAt = DateTime.Now
            };

            activity.Comments.Add(comment);

            var success = await _context.SaveChangesAsync(cancellationToken) > 0;

            if (success)
            {
                return(_mapper.Map <Comment, CommentDto>(comment));
            }

            throw new Exception("Problem saving changes.");
        }
Exemplo n.º 6
0
        public async Task <Unit> Handle(DeletePhotoCommand request, CancellationToken cancellationToken)
        {
            var user = await _context.Users.SingleOrDefaultAsync(u => u.UserName == _userAccessor.GetCurrentUsername(),
                                                                 cancellationToken);

            var photo = user.Photos.FirstOrDefault(p => p.Id == request.Id);

            if (photo == null)
            {
                throw new RestException(HttpStatusCode.NotFound, new { photo = "Could not find photo." });
            }
            if (photo.IsMain)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { photo = "You cannot delete your main photo." });
            }

            var result = _photoAccessor.DeletePhoto(photo.Id);

            if (result == null)
            {
                throw new Exception("Problem deleting the photo.");
            }

            user.Photos.Remove(photo);

            var success = await _context.SaveChangesAsync(cancellationToken) > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Problem saving changes.");
        }
Exemplo n.º 7
0
        public async Task <Unit> Handle(SetMainPhotoCommand request, CancellationToken cancellationToken)
        {
            var user = await _context.Users.SingleOrDefaultAsync(u => u.UserName == _userAccessor.GetCurrentUsername(),
                                                                 cancellationToken);

            var photo = user.Photos.FirstOrDefault(p => p.Id == request.Id);

            if (photo == null)
            {
                throw new RestException(HttpStatusCode.NotFound, new { Photo = "Could not find photo." });
            }

            var currentMain = user.Photos.FirstOrDefault(p => p.IsMain);

            if (currentMain != null)
            {
                currentMain.IsMain = false;
            }
            photo.IsMain = true;

            var success = await _context.SaveChangesAsync(cancellationToken) > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Problem saving changes.");
        }
        public async Task <Photo> Handle(AddPhotoCommand request, CancellationToken cancellationToken)
        {
            var photoUploadResult = _photoAccessor.AddPhoto(request.File);

            var user = await _context.Users.SingleOrDefaultAsync(u => u.UserName == _userAccessor.GetCurrentUsername(), cancellationToken);

            var photo = new Photo
            {
                Url = photoUploadResult.Url,
                Id  = photoUploadResult.PublicId
            };

            if (!user.Photos.Any(x => x.IsMain))
            {
                photo.IsMain = true;
            }

            user.Photos.Add(photo);

            var success = await _context.SaveChangesAsync(cancellationToken) > 0;

            if (success)
            {
                return(photo);
            }

            throw new Exception("Problem saving changes.");
        }
Exemplo n.º 9
0
            public async Task <Result <Photo> > Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.Include(p => p.Photos)
                           .FirstOrDefaultAsync(x => x.UserName == _userAccessor.GetUsername());

                if (user == null)
                {
                    return(null);
                }

                var photoUploadResult = await _photoAccessor.AddPhoto(request.File);

                var photo = new Photo
                {
                    Url = photoUploadResult.Url,
                    Id  = photoUploadResult.PublicId
                };

                if (!user.Photos.Any(x => x.IsMain))
                {
                    photo.IsMain = true;
                }

                user.Photos.Add(photo);

                var result = await _context.SaveChangesAsync() > 0;

                if (result)
                {
                    return(Result <Photo> .Success(photo));
                }

                return(Result <Photo> .Failure("Problem adding photo"));
            }
Exemplo n.º 10
0
            public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.Include(p => p.Photos)
                           .FirstOrDefaultAsync(x => x.UserName == _userAccessor.GetUsername());

                if (user == null)
                {
                    return(null);
                }

                var photo = user.Photos.FirstOrDefault(x => x.Id == request.Id);

                if (photo == null)
                {
                    return(null);
                }

                var currentMain = user.Photos.FirstOrDefault(x => x.IsMain);

                if (currentMain != null)
                {
                    currentMain.IsMain = false;
                }

                photo.IsMain = true;

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Result <Unit> .Success(Unit.Value));
                }

                return(Result <Unit> .Failure("Problem setting main photo"));
            }
Exemplo n.º 11
0
            public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
            {
                var activity = await _context.Activities
                               .Include(a => a.Attendees).ThenInclude(u => u.AppUser)
                               .SingleOrDefaultAsync(x => x.Id == request.Id);

                if (activity == null)
                {
                    return(null);
                }

                var user = await _context.Users
                           .FirstOrDefaultAsync(x => x.UserName == _userAccessor.GetUsername());

                if (user == null)
                {
                    return(null);
                }

                var hostUsername = activity.Attendees.FirstOrDefault(x => x.IsHost)?.AppUser?.UserName;

                var attendance = activity.Attendees.FirstOrDefault(x => x.AppUser.UserName == user.UserName);

                if (attendance != null && hostUsername == user.UserName)
                {
                    activity.IsCancelled = !activity.IsCancelled;
                }

                if (attendance != null && hostUsername != user.UserName)
                {
                    activity.Attendees.Remove(attendance);
                }

                if (attendance == null)
                {
                    attendance = new ActivityAttendee
                    {
                        AppUser  = user,
                        Activity = activity,
                        IsHost   = false
                    };

                    activity.Attendees.Add(attendance);
                }

                var result = await _context.SaveChangesAsync() > 0;

                return(result ? Result <Unit> .Success(Unit.Value) : Result <Unit> .Failure("Problem updating attendance"));
            }
Exemplo n.º 12
0
            public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.FirstOrDefaultAsync(x => x.UserName == _userAccessor.GetUsername());

                user.Bio         = request.Bio ?? user.Bio;
                user.DisplayName = request.DisplayName ?? user.DisplayName;

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Result <Unit> .Success(Unit.Value));
                }

                return(Result <Unit> .Failure("Problem updating profile"));
            }
Exemplo n.º 13
0
            public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
            {
                var activity = await _context.Activities.FindAsync(request.Id);

                if (activity == null)
                {
                    return(null);
                }

                _context.Remove(activity);

                var result = await _context.SaveChangesAsync() > 0;

                if (!result)
                {
                    return(Result <Unit> .Failure("Failed to delete the activity"));
                }

                return(Result <Unit> .Success(Unit.Value));
            }
Exemplo n.º 14
0
        public async Task <Unit> Handle(DeleteCommand request, CancellationToken cancellationToken)
        {
            var activity = await context.Activities.FindAsync(request.Id);

            if (activity == null)
            {
                throw new Exception("Activity not found");
            }

            context.Remove(activity);

            var result = await context.SaveChangesAsync() > 0;

            if (result)
            {
                return(Unit.Value);
            }

            throw new Exception("Problem saving changes");
        }
        public async Task <Unit> Handle(DeleteActivityCommand request, CancellationToken cancellationToken)
        {
            var activity = await _context.Activities.FindAsync(request.Id);

            if (activity == null)
            {
                throw new RestException(HttpStatusCode.NotFound, "Could not find activity.");
            }

            _context.Remove(activity);

            var success = await _context.SaveChangesAsync(cancellationToken) > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Error during save changes.");
        }
Exemplo n.º 16
0
        public async Task <Unit> Handle(AttendActivityCommand request, CancellationToken cancellationToken)
        {
            var activity = await _context.Activities.FindAsync(request.Id);

            if (activity == null)
            {
                throw new RestException(HttpStatusCode.NotFound, new { activity = "Could not find activity." });
            }

            var user = await _context.Users.SingleOrDefaultAsync(u => u.UserName == _userAccessor.GetCurrentUsername(),
                                                                 cancellationToken);

            var attendance =
                await _context.UserActivities.SingleOrDefaultAsync(
                    x => x.ActivityId == activity.Id && x.UserId == user.Id, cancellationToken);

            if (attendance != null)
            {
                throw new RestException(HttpStatusCode.BadRequest,
                                        new { Attendance = "Already attending this activity." });
            }

            attendance = new UserActivity
            {
                Activity   = activity,
                User       = user,
                IsHost     = false,
                DateJoined = DateTime.Now
            };

            _context.UserActivities.Add(attendance);

            var success = await _context.SaveChangesAsync(cancellationToken) > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Problem saving changes");
        }
Exemplo n.º 17
0
            public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.Include(p => p.Photos)
                           .FirstOrDefaultAsync(x => x.UserName == _userAccessor.GetUsername());

                if (user == null)
                {
                    return(null);
                }

                var photo = user.Photos.FirstOrDefault(x => x.Id == request.Id);

                if (photo == null)
                {
                    return(null);
                }

                if (photo.IsMain)
                {
                    return(Result <Unit> .Failure("You cannot delete your main photo"));
                }

                var result = await _photoAccessor.DeletePhoto(photo.Id);

                if (result == null)
                {
                    return(Result <Unit> .Failure("Problem deleting photo from cloudinary"));
                }

                user.Photos.Remove(photo);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Result <Unit> .Success(Unit.Value));
                }

                return(Result <Unit> .Failure("Problem deleting photo from API"));
            }
Exemplo n.º 18
0
        public async Task <Unit> Handle(CreateCommand request, CancellationToken cancellationToken)
        {
            var activity = new Activity
            {
                Id          = request.Id,
                Title       = request.Title,
                Description = request.Description,
                Category    = request.Category,
                Date        = request.Date,
                City        = request.City,
                Venue       = request.Venue,
            };

            context.Activities.Add(activity);
            var result = await context.SaveChangesAsync() > 0;

            if (result)
            {
                return(Unit.Value);
            }
            throw new Exception("PRoblem saving changes");
        }
Exemplo n.º 19
0
            public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
            {
                var observer = await _context.Users.FirstOrDefaultAsync(x =>
                                                                        x.UserName == _userAccessor.GetUsername());

                var target = await _context.Users.FirstOrDefaultAsync(x =>
                                                                      x.UserName == request.TargetUsername);

                if (target == null)
                {
                    return(null);
                }

                var following = await _context.UserFollowings.FindAsync(observer.Id, target.Id);

                if (following == null)
                {
                    following = new UserFollowing
                    {
                        Observer = observer,
                        Target   = target
                    };

                    _context.UserFollowings.Add(following);
                }
                else
                {
                    _context.UserFollowings.Remove(following);
                }

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Result <Unit> .Success(Unit.Value));
                }

                return(Result <Unit> .Failure("Failed to update following"));
            }
        public async Task <Unit> Handle(UnattendActivityCommand request, CancellationToken cancellationToken)
        {
            var activity = await _context.Activities.FindAsync(request.Id);

            if (activity == null)
            {
                throw new RestException(HttpStatusCode.NotFound, new { activity = "Could not find activity." });
            }

            var user = await _context.Users.SingleOrDefaultAsync(u => u.UserName == _userAccessor.GetCurrentUsername(),
                                                                 cancellationToken);

            var attendance =
                await _context.UserActivities.SingleOrDefaultAsync(
                    x => x.ActivityId == activity.Id && x.UserId == user.Id, cancellationToken);

            if (attendance == null)
            {
                return(Unit.Value);
            }

            if (attendance.IsHost)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { attendance = "You cannot remove yourself as host." });
            }

            _context.UserActivities.Remove(attendance);

            var success = await _context.SaveChangesAsync(cancellationToken) > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Problem saving changes.");
        }
        public async Task <Unit> Handle(CreateActivityCommand request, CancellationToken cancellationToken)
        {
            var activity = new Activity
            {
                Id          = request.Id,
                Name        = request.Name,
                Description = request.Description,
                Category    = request.Category,
                City        = request.City,
                Date        = request.Date,
                Venue       = request.Venue
            };

            _context.Activities.Add(activity);

            var user = await _context.Users.SingleOrDefaultAsync(u => u.UserName == _userAccessor.GetCurrentUsername(), cancellationToken);

            var attendee = new UserActivity
            {
                User       = user,
                Activity   = activity,
                IsHost     = true,
                DateJoined = DateTime.Now
            };

            _context.UserActivities.Add(attendee);

            var success = await _context.SaveChangesAsync(cancellationToken) > 0;

            if (success)
            {
                return(Unit.Value);
            }

            throw new Exception("Error during save changes.");
        }
Exemplo n.º 22
0
        public async Task <Unit> Handle(EditCommand request, CancellationToken cancellationToken)
        {
            var activity = await context.Activities.FindAsync(request.Id);

            if (activity == null)
            {
                throw new Exception("Activity not found");
            }

            activity.Title       = request.Title ?? activity.Title;
            activity.Description = request.Description ?? activity.Description;
            activity.Category    = request.Category ?? activity.Category;
            activity.Date        = request.Date ?? activity.Date;
            activity.City        = request.City ?? activity.Category;
            activity.Venue       = request.Venue ?? activity.Venue;

            var result = await context.SaveChangesAsync() > 0;

            if (result)
            {
                return(Unit.Value);
            }
            throw new Exception("Problem saving changes");
        }