Exemplo n.º 1
0
            public async Task <User> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _userManager.FindByNameAsync(_userAccessor.CurrentUsername());

                user.DisplayName = request.DisplayName ?? user.DisplayName;
                user.Bio         = request.Bio ?? user.Bio;
                if ((await _userManager.Users.AnyAsync(x => x.Email == request.Email) &&
                     request.Email != user.Email))
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { msg = "This Email Already Taken" });
                }
                user.Email = request.Email;
                var result = await _userManager.UpdateAsync(user);

                if (result.Succeeded)
                {
                    return(new User
                    {
                        DisplayName = user.DisplayName,
                        Username = user.UserName,
                        Email = user.Email,
                        Token = _jwtJenerator.CreateToken(user),
                        Image = user.Photos.FirstOrDefault(x => x.IsMain)?.Url
                    });
                }
                throw new Exception("Proplem Saving Changes");
            }
Exemplo n.º 2
0
        public bool Resolve(UserActivity source, AttendeeDto destination, bool destMember, ResolutionContext context)
        {
            var currentUser = _context.Users.SingleOrDefaultAsync(x => x.UserName ==
                                                                  _userAccessor.CurrentUsername()).Result;

            if (currentUser.Followings.Any(x => x.TargetId == source.UserId))
            {
                return(true);
            }
            return(false);
        }
Exemplo n.º 3
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                var user = await _userManager.FindByNameAsync(_userAccessor.CurrentUsername());

                return(new User
                {
                    Username = user.UserName,
                    DisplayName = user.DisplayName,
                    Email = user.Email,
                    Token = _jwtJenerator.CreateToken(user),
                    Image = user.Photos.FirstOrDefault(x => x.IsMain)?.Url
                });
            }
Exemplo n.º 4
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.CurrentUsername());

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

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

                throw new Exception("Problem saving changes");
            }
Exemplo n.º 5
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.BadRequest, new { msg = "Not Found Activity" });
                }
                var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.CurrentUsername());

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

                if (attendee != null)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { msg = "This Activity Already Attended" });
                }
                attendee = new UserActivity
                {
                    User       = user,
                    Activity   = activity,
                    DateJoined = DateTime.Now,
                    IsHost     = true
                };
                _context.UserActivities.Add(attendee);

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

                if (success)
                {
                    return(Unit.Value);
                }
                throw new Exception("Proplem Saving Changes");
            }
Exemplo n.º 6
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var activity = new Activity
                {
                    Id          = Guid.NewGuid(),
                    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.CurrentUsername());

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

                _context.UserActivities.Add(attendee);

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

                if (success)
                {
                    return(Unit.Value);
                }
                throw new Exception("Proplem Saving Changes");
            }
Exemplo n.º 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.BadRequest, new { msg = "Not Found Activity" });
                }
                var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.CurrentUsername());

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

                if (attendee == null)
                {
                    return(Unit.Value);
                }
                if (attendee.IsHost)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { msg = "You can not remove your self as host" });
                }
                _context.UserActivities.Remove(attendee);

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

                if (success)
                {
                    return(Unit.Value);
                }
                throw new Exception("Proplem Saving Changes");
            }
Exemplo n.º 8
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var observe = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.CurrentUsername());

                var target = await _context.Users.SingleOrDefaultAsync(x => x.UserName == request.Username);

                if (target == null)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { msg = "Not Found User" });
                }

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

                if (following != null)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { msg = "You are already following this user" });
                }
                else if (following == null)
                {
                    following = new UserFollowing
                    {
                        Observer = observe,
                        Target   = target
                    };
                    _context.Followings.Add(following);
                }
                var result = await _context.SaveChangesAsync() > 0;

                if (result)
                {
                    return(Unit.Value);
                }
                throw new Exception("Proplem Saving Changes");
            }
Exemplo n.º 9
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.CurrentUsername());

                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 your main photo" });
                }

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

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

                user.Photos.Remove(photo);

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

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

                throw new Exception("Problem saving changes");
            }
Exemplo n.º 10
0
        public async Task <Profile> ReadProfile(string username)
        {
            //user who went to show his profile
            var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == username);

            if (user == null)
            {
                throw new RestException(HttpStatusCode.NotFound, new{ msg = "Not Found User" });
            }
            var currentUser = await _context.Users.SingleOrDefaultAsync(x =>
                                                                        x.UserName == _userAccessor.CurrentUsername());

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

            if (currentUser.Followings.Any(x => x.TargetId == user.Id))
            {
                profile.IsFollowed = true;
            }
            return(profile);
        }
Exemplo n.º 11
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 { msg = "Activity Not Found" });
                }
                var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.CurrentUsername());

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

                if (attendee == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { msg = "You can not update this activity" });
                }
                activity.Title       = request.Title ?? activity.Title;
                activity.Description = request.Description ?? activity.Description;
                activity.City        = request.City ?? activity.City;
                activity.Venue       = request.Venue ?? activity.Venue;
                activity.Date        = request.Date ?? activity.Date;

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

                if (success)
                {
                    return(Unit.Value);
                }
                throw new Exception("Proplem Saving Changes");
            }
Exemplo n.º 12
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.CurrentUsername());

                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() > 0;

                if (success)
                {
                    return(photo);
                }

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