Beispiel #1
0
            public async Task <Photo> Handle(Command request, CancellationToken cancellationToken)
            {
                var photoUploadResult = _photoAccessor.AddPhoto(request.File);

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

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

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

                _context.Photos.Add(photo);
                var succes = await _context.SaveChangesAsync() > 0;

                if (succes)
                {
                    return(photo);
                }

                throw new Exception("Problem saving changes");
            }
Beispiel #2
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName ==
                                                                     _userAccessor.GetCurrentUserName());

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

                if (photo == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { Photo = "Not Found" });
                }

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

                currentMain.IsMain = false;
                photo.IsMain       = true;

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

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

                throw new Exception("Problem saving changes");
            }
        public bool Resolve(UserActivity source, AttendeeDto destination, bool destMember, ResolutionContext context)
        {
            var currentUser = _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUserName()).Result;

            if (currentUser.Followings.Any(x => x.TargetId == source.AppUserId))
            {
                return(true);
            }

            return(false);
        }
Beispiel #4
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                var user = await _userManager.FindByNameAsync(_userAccesssor.GetCurrentUserName());

                return(new User
                {
                    DisplayName = user.DisplayName,
                    UserName = user.UserName,
                    Token = _jwtGeneretaor.CreateToken(user),
                    Image = user.Photos.FirstOrDefault(x => x.IsMain)?.Url
                });
            }
Beispiel #5
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName ==
                                                                     _userAccessor.GetCurrentUserName());

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

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

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

                throw new Exception("Problem saving changes");
            }
Beispiel #6
0
            public async Task <Unit> Handle(Command 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(x =>
                                                                     x.UserName == _userAccessor.GetCurrentUserName());

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

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

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

                _context.UserActivities.Add(attendance);

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

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

                throw new Exception("Problem saving changes");
            }
Beispiel #7
0
            public async Task <Unit> Handle(Command 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(x =>
                                                                     x.UserName == _userAccessor.GetCurrentUserName());

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

                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 succes = await _context.SaveChangesAsync() > 0;

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

                throw new Exception("Problem saving changes");
            }
Beispiel #8
0
            public async Task <Unit> Handle(Command 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 user = await _context.Users.SingleOrDefaultAsync(x =>
                                                                     x.UserName == _userAccessor.GetCurrentUserName());

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

                _context.UserActivities.Add(attendee);

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

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

                throw new Exception("Problem saving changes");
            }
Beispiel #9
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName ==
                                                                     _userAccessor.GetCurrentUserName());

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

                if (photo == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { Photo = "Not Found" });
                }

                if (photo.IsMain)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Photo = "You cannot " +
                                                                                     "delete you main photo" });
                }

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

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

                user.Photos.Remove(photo);

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

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

                throw new Exception("Problem saving changes");
            }
Beispiel #10
0
        public async Task <Profile> ReadProfile(string username)
        {
            var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == username);

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

            var currentUser = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUserName());

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

            if (currentUser.Followings.Any(x => x.TargetId == user.Id))
            {
                profile.IsFollowed = true;
            }

            return(profile);
        }
Beispiel #11
0
            public async Task <ActivitiesEnvelope> Handle(Query request, CancellationToken cancellationToken)
            {
                var queryable = _context.Activities
                                .Where(x => x.Date >= request.StartDate)
                                .OrderBy(x => x.Date)
                                .AsQueryable();

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

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

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


                return(new ActivitiesEnvelope
                {
                    Activities = _mapper.Map <List <Activity>, List <ActivityDto> >(activities),
                    ActivityCount = queryable.Count()
                });
            }
Beispiel #12
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                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 RestException(HttpStatusCode.NotFound, new { User = "******" });
                }

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

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

                following = new UserFollowing
                {
                    Target   = target,
                    Observer = observer
                };

                _context.Followings.Add(following);

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

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

                throw new Exception("Problem saving changes");
            }