コード例 #1
0
        public async Task <Unit> Handle(CreateComment request, CancellationToken cancellationToken)
        {
            var username = await _userAccessor.GetUsername();

            var creator = await _context.Creators.SingleOrDefaultAsync(c => c.Username.ToLower() == username.ToLower(),
                                                                       cancellationToken);

            if (creator == null)
            {
                throw new ApplicationException($"Could not find requested creator {username}");
            }

            var article = await _context.Articles.SingleOrDefaultAsync(a => a.Id == request.ArticleId,
                                                                       cancellationToken);

            if (article == null)
            {
                throw new ApplicationException($"Article with id : {request.ArticleId} could not be found");
            }

            var comment = new Comment(request.Content, creator);

            article.AddComment(comment);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #2
0
        public async Task <Unit> Handle(CreateNewUser request, CancellationToken cancellationToken)
        {
            ValidateRequest();
            var ipAddress = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
            var user      = RegisteredUser.CreateAccount(request.Username, request.Password, request.Email, ipAddress,
                                                         request.UserType);
            await _context.Users.AddAsync(user, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);

            void ValidateRequest()
            {
                if (string.IsNullOrEmpty(request.Username))
                {
                    throw new ApplicationException("Username cannot be null");
                }
                if (string.IsNullOrEmpty(request.Password))
                {
                    throw new ApplicationException("Password cannot be null");
                }
                if (string.IsNullOrEmpty(request.Email))
                {
                    throw new ApplicationException("Email cannot be null");
                }
            }
        }
コード例 #3
0
        public async Task <IActionResult> Get()
        {
            var creator = new Moderator("un", "pw", "dd", "::1");
            await _context.Users.AddAsync(creator);

            await _context.SaveChangesAsync();

            var users = await _context.Users.ToListAsync();

            return(Ok(users));
        }
コード例 #4
0
        public async Task <Unit> Handle(BanUser request, CancellationToken cancellationToken)
        {
            var username = await _userAccessor.GetUsername();

            var moderator = await _context.Moderators.SingleAsync(m => m.Username == username, cancellationToken);

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

            moderator.BanUser(user);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #5
0
        public async Task <Unit> Handle(CreateNewAd request, CancellationToken cancellationToken)
        {
            var username = await _userAccessor.GetUsername();

            var advertiser = await _context.Advertisers.SingleOrDefaultAsync(a => a.Username.ToLower() == username.ToLower(),
                                                                             cancellationToken);

            var ad = Ad.Create(request.Path, request.Price, advertiser);
            await _context.AddAsync(ad, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #6
0
        public async Task <Unit> Handle(DeleteCategory request, CancellationToken cancellationToken)
        {
            var category = await _context.Categories.SingleOrDefaultAsync(c => c.Id == request.CategoryId,
                                                                          cancellationToken);

            if (category == null)
            {
                throw new ApplicationException($"Category with id {request.CategoryId} could not be found");
            }

            _context.Categories.Remove(category);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #7
0
        public async Task <Unit> Handle(CreateNewCategory request, CancellationToken cancellationToken)
        {
            var exists = _context.Categories.Any(c => c.Name.ToLower() == request.Name.ToLower());

            if (exists)
            {
                throw new ApplicationException("Category with this name already exists");
            }
            var category = new Category(request.Name, request.Description);
            await _context.Categories.AddAsync(category, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #8
0
        public async Task <Unit> Handle(DeleteArticle request, CancellationToken cancellationToken)
        {
            var article = await _context.Articles.SingleOrDefaultAsync(a => a.Id == request.ArticleId,
                                                                       cancellationToken);

            if (article == null)
            {
                throw new ApplicationException("Article not found");
            }

            _context.Articles.Remove(article);
            article.Delete();
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #9
0
        public async Task <Unit> Handle(UpdateArticle request, CancellationToken cancellationToken)
        {
            var article = await _context.Articles.Include(a => a.Ads)
                          .SingleOrDefaultAsync(a => a.Id == request.ArticleId, cancellationToken);

            var ads = await _context.Ads.Where(a => request.AdIds.Contains(a.Id))
                      .ToListAsync(cancellationToken);

            if (article == null)
            {
                throw new ApplicationException($"Cannot find such article {request.ArticleId}");
            }

            article.AddAds(ads);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #10
0
        public async Task <Unit> Handle(UpdateAd request, CancellationToken cancellationToken)
        {
            var username = await _userAccessor.GetUsername();

            var advertiser = await _context.Advertisers.SingleAsync(a => a.Username.ToLower() == username.ToLower(),
                                                                    cancellationToken : cancellationToken);

            var ad = await _context.Ads.SingleOrDefaultAsync(a => a.Id == request.Id,
                                                             cancellationToken : cancellationToken);

            if (ad == null)
            {
                throw new ApplicationException("Could not find an ad with given id");
            }

            ad.Edit(advertiser, request.Path, request.Price);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #11
0
        public async Task <Unit> Handle(CreateNewArticle request, CancellationToken cancellationToken)
        {
            var username = await _userAccessor.GetUsername();

            var creator = await _context.Creators.SingleOrDefaultAsync(u => u.Username.ToLower() == username.ToLower(),
                                                                       cancellationToken);

            if (creator == null)
            {
                throw new ApplicationException("No such creator");
            }

            var category = await _context.Categories.SingleOrDefaultAsync(c => c.Id == request.CategoryId,
                                                                          cancellationToken);

            var article = new Article(request.Title, request.Description, request.Body, creator, category);
            await _context.Articles.AddAsync(article, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #12
0
        public async Task <ArticleDto> Handle(GetArticle request, CancellationToken cancellationToken)
        {
            var article = await _context.Articles
                          .Include(a => a.Ads)
                          .Include(a => a.Comments)
                          .Include(a => a.Category)
                          .Include(a => a.ContentVisitors)
                          .SingleOrDefaultAsync(a => a.Id == request.ArticleId, cancellationToken);

            var username = await _userAccessor.GetUsername();

            var ipAddress = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress;

            if (string.IsNullOrEmpty(username))
            {
                var visitor = new Visitor(ipAddress.ToString());
                var visit   = new ContentVisitor(article, visitor);
                visitor.AddVisit(visit);
                await _context.Visitors.AddAsync(visitor, cancellationToken);
            }
            else
            {
                var user = await _context.Users.SingleOrDefaultAsync(u => u.Username.ToLower() == username,
                                                                     cancellationToken);

                if (user == null)
                {
                    throw new ApplicationException($"Could not find such user {username}");
                }

                var visit = new ContentVisitor(article, user);
                user.AddVisit(visit);
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(article.AsDto());
        }