public bool Resolve(UserActivity source, AttendeeDTO destination, bool destMember, ResolutionContext context)
        {
            //get user
            var user = _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUserName()).Result;

            if (user.Followings.Any(x => x.TargetId == source.AppUserId))
            {
                return(true);
            }
            return(false);
        }
Example #2
0
            public async Task <UserDTO> Handle(Query request, CancellationToken cancellationToken)
            {
                var user = await _userManager.FindByNameAsync(_userAccessor.GetCurrentUserName());

                return(new UserDTO
                {
                    DisplayName = user.DisplayName,
                    Token = _jwtGenerator.CreateToken(user),
                    RefreshToken = user.RefreshToken,
                    UserName = user.UserName,
                    Image = user.Photos.FirstOrDefault(x => x.IsMain)?.Url,
                });
            }
Example #3
0
        public async Task <Profile> ReadProfile(string userName)
        {
            //get target user
            var target = await _context.Users.SingleOrDefaultAsync(x => x.UserName == userName);

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

            //get current user
            var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUserName());

            //create profile
            var profile = new Profile
            {
                DisplayName    = target.DisplayName,
                UserName       = target.UserName,
                Image          = target.Photos.FirstOrDefault(x => x.IsMain)?.Url,
                Bio            = target.Bio,
                Photos         = target.Photos,
                FollowersCount = target.Followers.Count(),
                FollowingCount = target.Followings.Count()
            };

            //determine if following user
            if (user.Followings.Any(x => x.TargetId == target.Id))
            {
                profile.IsFollowed = true;
            }
            else
            {
                profile.IsFollowed = false;
            }

            return(profile);
        }
Example #4
0
            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.");
            }
Example #5
0
            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.");
            }
Example #6
0
            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.");
            }
Example #7
0
            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.");
            }
Example #8
0
            //implement interface
            //cancellation token is sent when a user sends a request, but then either refreshes or leaves page
            public async Task <ActivitiesEnvelope> Handle(Query request, CancellationToken cancellationToken)
            {
                //get IQueryable of activities sorted by date
                var queryable = _context.Activities
                                .Where(a => a.Date >= request.StartDate)
                                .OrderBy(a => a.Date)
                                .AsQueryable();

                //get activities user is attending
                if (request.IsGoing && !request.IsHost)
                {
                    queryable = queryable.Where(x => x.UserActivities.Any(a => a.AppUser.UserName == _userAccessor.GetCurrentUserName()));
                }

                if (request.IsHost)
                {
                    queryable = queryable.Where(x => x.UserActivities.Any(a => a.IsHost && a.AppUser.UserName == _userAccessor.GetCurrentUserName()));
                }

                var activities = await queryable
                                 .Skip(request.Offset ?? 0)
                                 .Take(request.Limit ?? 3)
                                 .ToListAsync();

                return(new ActivitiesEnvelope
                {
                    Activities = _mapper.Map <List <Domain.Activity>, List <ActivityDTO> >(activities),
                    ActivityCount = queryable.Count()
                });

                //eager loading related data - when we get activities, we also get user activities and app users - requires include statement
                // var activities = await _context.Activities
                // .Include(x => x.UserActivities)
                // .ThenInclude(x => x.AppUser)
                // .ToListAsync();

                //lazy loading related data
                // var activities = await _context.Activities.ToListAsync();
            }
Example #9
0
            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 not attending activity." });
                }

                //ensure host does not leave activity
                if (attendance.IsHost)
                {
                    throw new Application.Errors.RestException(HttpStatusCode.BadRequest, new { attendance = "Host must attend activity." });
                }

                //delete attendance
                _context.UserActivities.Remove(attendance);

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

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

                throw new Exception("Problem saving changes.");
            }
Example #10
0
            //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.");
            }
Example #11
0
            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.");
            }