Exemple #1
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var observer = await _context.Users.SingleOrDefaultAsync(observer => observer.UserName == _userAccessor.GetCurrentUsername());

                var target = await _context.Users.SingleOrDefaultAsync(user => user.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 = "******" });
                }

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

                    _context.Followings.Add(following);
                }

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

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

                throw new Exception("Problem saving changes");
            }
Exemple #2
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 Exception("NOt found");
                }
                var following = await _context.Followings.SingleOrDefaultAsync(x => x.ObserverId == observer.Id && x.TargetId == target.Id);

                if (following != null)
                {
                    throw new Exception("You are following this user");
                }

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

                    _context.Followings.Add(following);
                }

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

                if (success)
                {
                    return(Unit.Value);        //prazan objekt(vraća 200 ok)
                }
                throw new Exception("Probblem saving changes");
            }
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var smth     = _userAccessor.GetCurrentUserId();
                var smthelse = _userAccessor.GetCurrentUsername();
                var observer = await _userRepo.GetSingleOrDefaultAsync(x => x.Id == _userAccessor.GetCurrentUserId());

                var target = await _userRepo.GetSingleOrDefaultAsync(x => x.UserName == request.Username);

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

                var following = await _followRepo.GetSingleOrDefaultAsync(x => x.ObserverId == observer.Id && x.TargetId == target.Id);

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

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

                _followRepo.Add(following);

                var succcess = await _followRepo.SaveAsync();

                if (!succcess)
                {
                    throw new Exception("Problem saving changes");
                }

                return(Unit.Value);
            }
            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 updated following"));
            }
        public async Task <Result <Unit> > Handle(CreateFollowerCommand request, CancellationToken cancellationToken)
        {
            var observer = await _userManager.FindByNameAsync(_userAccessor.GetUsername());

            var target = await _userManager.FindByNameAsync(request.TargetUserName);

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

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

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

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

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

            if (!success)
            {
                return(Result <Unit> .Failure("Failed to update following"));
            }

            return(Result <Unit> .Success(Unit.Value));
        }
Exemple #6
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var usersInfo = await this._context.Users.Where(t => t.UserName == request.UserName || t.UserName == this._userAccessor.GetCurrentUserName())
                                .ToListAsync();

                var observer = usersInfo.FirstOrDefault(t => t.UserName == this._userAccessor.GetCurrentUserName());
                var target   = usersInfo.FirstOrDefault(t => t.UserName == request.UserName);

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

                if (await this._context.Followings.AnyAsync(t => t.ObserverId == observer.Id && t.TargetId == target.Id))
                {
                    throw new RestException(HttpStatusCode.UnprocessableEntity, new {
                        Following = "Already following the follower"
                    });
                }

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

                this._context.Followings.Add(following);

                if (await this._context.SaveChangesAsync() > 0)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem occured while saving the request");
            }
Exemple #7
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 { Users = "You are already following this user." });
                }
                else
                {
                    following = new UserFollowing
                    {
                        Observer = observer,
                        Target   = target
                    };
                }

                _context.Followings.Add(following);

                // logic goes here
                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }
                throw new Exception("An error occured when saving activity to the database.");
            }
Exemple #8
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                // Current user would be observer.
                var observer = await _context.Users.SingleOrDefaultAsync(x => x.Email == _userAccessor.GetEmail(), cancellationToken : cancellationToken);

                var target = await _context.Users.SingleOrDefaultAsync(x => x.Email == request.Email, cancellationToken : cancellationToken);

                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, cancellationToken : cancellationToken);

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

                following = new UserFollowing
                {
                    Observer = observer,
                    Target   = target
                };
                await _context.Followings.AddAsync(following, cancellationToken);


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

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

                throw new Exception("Problem following the user.");
            }
Exemple #9
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var observer = await context.Users.SingleOrDefaultAsync(u => u.UserName == userAccessor.GetCurrentUsername());

                var target = await context.Users.SingleOrDefaultAsync(u => u.UserName == request.Username);

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

                if (following != null)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
                }
                else
                {
                    following = new UserFollowing
                    {
                        Observer = observer,
                        Target   = target
                    };
                    context.Followings.Add(following);
                }

                var success = await context.SaveChangesAsync() > 0; // SaveChangesAsync returns # of changes persisted to DB.

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

                throw new Exception("Problem saving following");
            }
Exemple #10
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var userObserver = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _accessor.getUsername());

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

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

                //check to see if there is exsisting following

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

                if (following != null)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Error = "Already following this user" });
                }

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

                _context.Followings.Add(following);

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

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

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