Beispiel #1
0
        public async Task <Unit> Handle(FollowUserCommand request, CancellationToken cancellationToken)
        {
            var entity = _mapper.Map <Following>(request);

            _context.Followings.Add(entity);

            if (await _context.SaveChangesAsync(cancellationToken) > 0)
            {
                var setting = _context.UserNotificationSettings
                              .Where(x => x.UserId == entity.FollowerId)
                              .FirstOrDefault()
                              .UserFollowings;

                if (setting)
                {
                    if (entity.FollowerId != entity.UserId)
                    {
                        var command = new CreateNotificationCommand
                        {
                            UserId           = entity.FollowerId,
                            RecipientId      = entity.UserId,
                            NotificationType = NotificationType.UserFollowing
                        };

                        await _mediator.Send(command);
                    }
                }
            }

            return(Unit.Value);
        }
        public async Task <Unit> Handle(DeletePostCommand request, CancellationToken cancellationToken)
        {
            var entity = await _context.Posts
                         .Include(x => x.PostComments)
                         .Include(x => x.PostReports)
                         .Include(x => x.PostLikes)
                         .FirstOrDefaultAsync(x => x.Id == request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Post), request.Id);
            }

            _imageService.DeleteImage(entity.PhotoPath);

            _context.Comments.RemoveRange(entity.PostComments);
            _context.Reports.RemoveRange(entity.PostReports);
            _context.Likes.RemoveRange(entity.PostLikes);

            _context.Posts.Remove(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(UpdateAccountCommand request, CancellationToken cancellationToken)
        {
            var id = int.Parse(_userService.GetUserId());

            var entity = await _context.Users.FindAsync(id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(User), id);
            }

            if (request.Avatar != null)
            {
                string fileName = _imageService.SaveImage(request.Avatar);
                //await request.Avatar.CopyToAsync(new FileStream(filePath, FileMode.Create));

                if (entity.AvatarPath != null)
                {
                    _imageService.DeleteImage(entity.AvatarPath);
                }

                entity.AvatarPath = fileName;
            }

            _mapper.Map(request, entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(CreateCountryCommand request, CancellationToken cancellationToken)
        {
            var entity = _mapper.Map <Country>(request);

            _context.Countries.Add(entity);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Beispiel #5
0
        public async Task <Unit> Handle(CreateCommentReportCommand request, CancellationToken cancellationToken)
        {
            var entity = _mapper.Map <CommentReport>(request);

            entity.AccuserId = int.Parse(_userService.GetUserId());

            _context.CommentReports.Add(entity);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(DeleteAllNotificationsCommand request, CancellationToken cancellationToken)
        {
            var id = int.Parse(_userService.GetUserId());

            var entities = _context.Notifications
                           .Where(x => x.RecipientId == id).ToList();

            _context.Notifications.RemoveRange(entities);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Beispiel #7
0
        public async Task <Unit> Handle(UpdatePostCommand request, CancellationToken cancellationToken)
        {
            var entity = await _context.Posts.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Post), request.Id);
            }

            _mapper.Map(request, entity);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Beispiel #8
0
        public async Task <Unit> Handle(DeleteNotificationCommand request, CancellationToken cancellationToken)
        {
            var entity = _context.Notifications.Find(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Notification), request.Id);
            }

            _context.Notifications.Remove(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Beispiel #9
0
        public async Task <Unit> Handle(BlockUserCommand request, CancellationToken cancellationToken)
        {
            var entity = await _context.Users.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(User), request.Id);
            }

            entity.LockoutEnd = DateTime.MaxValue;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Beispiel #10
0
        public async Task <Unit> Handle(ReadNotificationsCommand request, CancellationToken cancellationToken)
        {
            var id = int.Parse(_userService.GetUserId());

            var entities = _context.Notifications
                           .Where(x => x.RecipientId == id && x.IsRead == false).ToList();

            foreach (var entity in entities)
            {
                entity.IsRead = true;
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Beispiel #11
0
        public async Task <Unit> Handle(CreatePostCommand request, CancellationToken cancellationToken)
        {
            var entity = _mapper.Map <Post>(request);

            entity.UserId = int.Parse(_userService.GetUserId());

            if (request.Photo != null)
            {
                string fileName = _imageService.SaveImage(request.Photo);
                entity.PhotoPath = fileName;
            }

            _context.Posts.Add(entity);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Beispiel #12
0
        public async Task <Unit> Handle(DeleteAccountCommand request, CancellationToken cancellationToken)
        {
            var id = int.Parse(_userService.GetUserId());

            var entity = await _context.Users
                         .Include(x => x.Comments)
                         .Include(x => x.Posts)
                         .ThenInclude(x => x.PostComments)
                         .Include(x => x.Posts)
                         .ThenInclude(x => x.PostReports)
                         .Include(x => x.Posts)
                         .ThenInclude(x => x.PostLikes)
                         .Include(x => x.Followings)
                         .Include(x => x.NotificationsTo)
                         .FirstOrDefaultAsync(x => x.Id == id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(User), id);
            }

            if (entity.AvatarPath != null)
            {
                _imageService.DeleteImage(entity.AvatarPath);
            }

            foreach (var post in entity.Posts)
            {
                _imageService.DeleteImage(post.PhotoPath);

                _context.Comments.RemoveRange(post.PostComments);
                _context.Reports.RemoveRange(post.PostReports);
                _context.Likes.RemoveRange(post.PostLikes);
            }

            _context.Comments.RemoveRange(entity.Comments);
            _context.Followings.RemoveRange(entity.Followings);
            _context.Notifications.RemoveRange(entity.NotificationsTo);

            _context.Users.Remove(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Beispiel #13
0
        public async Task <Unit> Handle(DeletePostReportsCommand request, CancellationToken cancellationToken)
        {
            var entities = _context.PostReports.Where(x => x.PostId == request.Id).ToList();

            if (entities == null)
            {
                throw new NotFoundException(nameof(CommentReport), request.Id);
            }

            foreach (var entity in entities)
            {
                _context.PostReports.Remove(entity);
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Beispiel #14
0
        public async Task <Unit> Handle(CreateCommentLikeCommand request, CancellationToken cancellationToken)
        {
            var entity = _mapper.Map <CommentLike>(request);

            entity.UserId = int.Parse(_userService.GetUserId());

            _context.CommentLikes.Add(entity);

            if (await _context.SaveChangesAsync(cancellationToken) > 0)
            {
                var item = _context.CommentLikes
                           .Where(x => x.CommentId == entity.CommentId && x.UserId == entity.UserId)
                           .FirstOrDefault();

                await _wallService.SendCommentLike(entity.UserId, item.Id, item.CommentId);

                var recipientId = _context.Comments.Find(entity.CommentId)?.UserId;

                if (recipientId != null)
                {
                    var setting = _context.UserNotificationSettings
                                  .Where(x => x.UserId == recipientId)
                                  .FirstOrDefault()
                                  .CommentLikes;

                    if (setting)
                    {
                        if (entity.UserId != recipientId)
                        {
                            var command = new CreateNotificationCommand
                            {
                                UserId           = entity.UserId,
                                RecipientId      = recipientId.Value,
                                NotificationType = NotificationType.CommentLike
                            };

                            await _mediator.Send(command);
                        }
                    }
                }
            }

            return(Unit.Value);
        }
Beispiel #15
0
        public async Task <Unit> Handle(UpdateNotificationsCommand request, CancellationToken cancellationToken)
        {
            var id = int.Parse(_userService.GetUserId());

            var entity = _context.UserNotificationSettings
                         .Where(x => x.UserId == id)
                         .FirstOrDefault();

            if (entity == null)
            {
                throw new NotFoundException(nameof(UserNotificationSettings), id);
            }

            _mapper.Map(request, entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(DeleteAvatarCommand request, CancellationToken cancellationToken)
        {
            var id = int.Parse(_userService.GetUserId());

            var entity = await _context.Users.FindAsync(id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(User), id);
            }

            if (entity.AvatarPath != null)
            {
                _imageService.DeleteImage(entity.AvatarPath);
                entity.AvatarPath = null;
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Beispiel #17
0
        public async Task <Unit> Handle(CreateNotificationCommand request, CancellationToken cancellationToken)
        {
            var entity = _mapper.Map <Notification>(request);

            _context.Notifications.Add(entity);

            if (await _context.SaveChangesAsync(cancellationToken) > 0)
            {
                var item = _context.Notifications
                           .Where(x => x.UserId == entity.UserId)
                           .Where(x => x.RecipientId == entity.RecipientId)
                           .Where(x => x.CreatedAt == entity.CreatedAt)
                           .Include(x => x.User)
                           .FirstOrDefault();

                var notification = _mapper.Map <UnreadNotificationDto>(item);

                await _notificationService.SendNotification(notification);
            }

            return(Unit.Value);
        }
        public async Task <Unit> Handle(UnfollowUserCommand request, CancellationToken cancellationToken)
        {
            var entity = _context.Followings
                         .Where(x => x.UserId == request.UserId && x.FollowerId == request.FollowerId)
                         .FirstOrDefault();

            if (entity == null)
            {
                throw new NotFoundException(nameof(Following), $"UserId: { request.UserId }, FollowerId: { request.FollowerId }");
            }

            _context.Followings.Remove(entity);

            if (await _context.SaveChangesAsync(cancellationToken) > 0)
            {
                var setting = _context.UserNotificationSettings
                              .Where(x => x.UserId == entity.FollowerId)
                              .FirstOrDefault()
                              .UserFollowings;

                if (setting)
                {
                    if (entity.FollowerId != entity.UserId)
                    {
                        var command = new CreateNotificationCommand
                        {
                            UserId           = entity.FollowerId,
                            RecipientId      = entity.UserId,
                            NotificationType = NotificationType.UserUnfollowing
                        };

                        await _mediator.Send(command);
                    }
                }
            }

            return(Unit.Value);
        }
        private async Task SeedCustomersAsync(CancellationToken cancellationToken)
        {
            var countries = new[]
            {
                new Country {
                    Name = "Afghanistan"
                },
                new Country {
                    Name = "Albania"
                },
                new Country {
                    Name = "Algeria"
                },
                new Country {
                    Name = "Andorra"
                },
                new Country {
                    Name = "Angola"
                },
                new Country {
                    Name = "Antigua and Barbuda"
                },
                new Country {
                    Name = "Argentina"
                },
                new Country {
                    Name = "Armenia"
                },
                new Country {
                    Name = "Australia"
                },
                new Country {
                    Name = "Austria"
                },
                new Country {
                    Name = "Azerbaijan"
                },
                new Country {
                    Name = "Bahamas"
                },
                new Country {
                    Name = "Bahrain"
                },
                new Country {
                    Name = "Bangladesh"
                },
                new Country {
                    Name = "Barbados"
                },
                new Country {
                    Name = "Belarus"
                },
                new Country {
                    Name = "Belgium"
                },
                new Country {
                    Name = "Belize"
                },
                new Country {
                    Name = "Benin"
                },
                new Country {
                    Name = "Bhutan"
                },
                new Country {
                    Name = "Bolivia"
                },
                new Country {
                    Name = "Bosnia and Herzegovina"
                },
                new Country {
                    Name = "Botswana"
                },
                new Country {
                    Name = "Brazil"
                },
                new Country {
                    Name = "Brunei"
                },
                new Country {
                    Name = "Bulgaria"
                },
                new Country {
                    Name = "Burkina Faso"
                },
                new Country {
                    Name = "Burundi"
                },
                new Country {
                    Name = "Côte d'Ivoire"
                },
                new Country {
                    Name = "Cabo Verde"
                },
                new Country {
                    Name = "Cambodia"
                },
                new Country {
                    Name = "Cameroon"
                },
                new Country {
                    Name = "Canada"
                },
                new Country {
                    Name = "Central African Republic"
                },
                new Country {
                    Name = "Chad"
                },
                new Country {
                    Name = "Chile"
                },
                new Country {
                    Name = "China"
                },
                new Country {
                    Name = "Colombia"
                },
                new Country {
                    Name = "Comoros"
                },
                new Country {
                    Name = "Congo"
                },
                new Country {
                    Name = "Costa Rica"
                },
                new Country {
                    Name = "Croatia"
                },
                new Country {
                    Name = "Cuba"
                },
                new Country {
                    Name = "Cyprus"
                },
                new Country {
                    Name = "Czechia"
                },
                new Country {
                    Name = "Democratic Republic of the Congo"
                },
                new Country {
                    Name = "Denmark"
                },
                new Country {
                    Name = "Djibouti"
                },
                new Country {
                    Name = "Dominica"
                },
                new Country {
                    Name = "Dominican Republic"
                },
                new Country {
                    Name = "Ecuador"
                },
                new Country {
                    Name = "Egypt"
                },
                new Country {
                    Name = "El Salvador"
                },
                new Country {
                    Name = "Equatorial Guinea"
                },
                new Country {
                    Name = "Eritrea"
                },
                new Country {
                    Name = "Estonia"
                },
                new Country {
                    Name = "Eswatini"
                },
                new Country {
                    Name = "Ethiopia"
                },
                new Country {
                    Name = "Fiji"
                },
                new Country {
                    Name = "Finland"
                },
                new Country {
                    Name = "France"
                },
                new Country {
                    Name = "Gabon"
                },
                new Country {
                    Name = "Gambia"
                },
                new Country {
                    Name = "Georgia"
                },
                new Country {
                    Name = "Germany"
                },
                new Country {
                    Name = "Ghana"
                },
                new Country {
                    Name = "Greece"
                },
                new Country {
                    Name = "Grenada"
                },
                new Country {
                    Name = "Guatemala"
                },
                new Country {
                    Name = "Guinea"
                },
                new Country {
                    Name = "Guinea-Bissau"
                },
                new Country {
                    Name = "Guyana"
                },
                new Country {
                    Name = "Haiti"
                },
                new Country {
                    Name = "Holy See"
                },
                new Country {
                    Name = "Honduras"
                },
                new Country {
                    Name = "Hungary"
                },
                new Country {
                    Name = "Iceland"
                },
                new Country {
                    Name = "India"
                },
                new Country {
                    Name = "Indonesia"
                },
                new Country {
                    Name = "Iran"
                },
                new Country {
                    Name = "Iraq"
                },
                new Country {
                    Name = "Ireland"
                },
                new Country {
                    Name = "Israel"
                },
                new Country {
                    Name = "Italy"
                },
                new Country {
                    Name = "Jamaica"
                },
                new Country {
                    Name = "Japan"
                },
                new Country {
                    Name = "Jordan"
                },
                new Country {
                    Name = "Kazakhstan"
                },
                new Country {
                    Name = "Kenya"
                },
                new Country {
                    Name = "Kiribati"
                },
                new Country {
                    Name = "Kuwait"
                },
                new Country {
                    Name = "Kyrgyzstan"
                },
                new Country {
                    Name = "Laos"
                },
                new Country {
                    Name = "Latvia"
                },
                new Country {
                    Name = "Lebanon"
                },
                new Country {
                    Name = "Lesotho"
                },
                new Country {
                    Name = "Liberia"
                },
                new Country {
                    Name = "Libya"
                },
                new Country {
                    Name = "Liechtenstein"
                },
                new Country {
                    Name = "Lithuania"
                },
                new Country {
                    Name = "Luxembourg"
                },
                new Country {
                    Name = "Madagascar"
                },
                new Country {
                    Name = "Malawi"
                },
                new Country {
                    Name = "Malaysia"
                },
                new Country {
                    Name = "Maldives"
                },
                new Country {
                    Name = "Mali"
                },
                new Country {
                    Name = "Malta"
                },
                new Country {
                    Name = "Marshall Islands"
                },
                new Country {
                    Name = "Mauritania"
                },
                new Country {
                    Name = "Mauritius"
                },
                new Country {
                    Name = "Mexico"
                },
                new Country {
                    Name = "Micronesia"
                },
                new Country {
                    Name = "Moldova"
                },
                new Country {
                    Name = "Monaco"
                },
                new Country {
                    Name = "Mongolia"
                },
                new Country {
                    Name = "Montenegro"
                },
                new Country {
                    Name = "Morocco"
                },
                new Country {
                    Name = "Mozambique"
                },
                new Country {
                    Name = "Myanmar"
                },
                new Country {
                    Name = "Namibia"
                },
                new Country {
                    Name = "Nauru"
                },
                new Country {
                    Name = "Nepal"
                },
                new Country {
                    Name = "Netherlands"
                },
                new Country {
                    Name = "New Zealand"
                },
                new Country {
                    Name = "Nicaragua"
                },
                new Country {
                    Name = "Niger"
                },
                new Country {
                    Name = "Nigeria"
                },
                new Country {
                    Name = "North Korea"
                },
                new Country {
                    Name = "North Macedonia"
                },
                new Country {
                    Name = "Norway"
                },
                new Country {
                    Name = "Oman"
                },
                new Country {
                    Name = "Pakistan"
                },
                new Country {
                    Name = "Palau"
                },
                new Country {
                    Name = "Palestine State"
                },
                new Country {
                    Name = "Panama"
                },
                new Country {
                    Name = "Papua New Guinea"
                },
                new Country {
                    Name = "Paraguay"
                },
                new Country {
                    Name = "Peru"
                },
                new Country {
                    Name = "Philippines"
                },
                new Country {
                    Name = "Poland"
                },
                new Country {
                    Name = "Portugal"
                },
                new Country {
                    Name = "Qatar"
                },
                new Country {
                    Name = "Romania"
                },
                new Country {
                    Name = "Russia"
                },
                new Country {
                    Name = "Rwanda"
                },
                new Country {
                    Name = "Saint Kitts and Nevis"
                },
                new Country {
                    Name = "Saint Lucia"
                },
                new Country {
                    Name = "Saint Vincent and the Grenadines"
                },
                new Country {
                    Name = "Samoa"
                },
                new Country {
                    Name = "San Marino"
                },
                new Country {
                    Name = "Sao Tome and Principe"
                },
                new Country {
                    Name = "Saudi Arabia"
                },
                new Country {
                    Name = "Senegal"
                },
                new Country {
                    Name = "Serbia"
                },
                new Country {
                    Name = "Seychelles"
                },
                new Country {
                    Name = "Sierra Leone"
                },
                new Country {
                    Name = "Singapore"
                },
                new Country {
                    Name = "Slovakia"
                },
                new Country {
                    Name = "Slovenia"
                },
                new Country {
                    Name = "Solomon Islands"
                },
                new Country {
                    Name = "Somalia"
                },
                new Country {
                    Name = "South Africa"
                },
                new Country {
                    Name = "South Korea"
                },
                new Country {
                    Name = "South Sudan"
                },
                new Country {
                    Name = "Spain"
                },
                new Country {
                    Name = "Sri Lanka"
                },
                new Country {
                    Name = "Sudan"
                },
                new Country {
                    Name = "Suriname"
                },
                new Country {
                    Name = "Sweden"
                },
                new Country {
                    Name = "Switzerland"
                },
                new Country {
                    Name = "Syria"
                },
                new Country {
                    Name = "Tajikistan"
                },
                new Country {
                    Name = "Tanzania"
                },
                new Country {
                    Name = "Thailand"
                },
                new Country {
                    Name = "Timor-Leste"
                },
                new Country {
                    Name = "Togo"
                },
                new Country {
                    Name = "Tonga"
                },
                new Country {
                    Name = "Trinidad and Tobago"
                },
                new Country {
                    Name = "Tunisia"
                },
                new Country {
                    Name = "Turkey"
                },
                new Country {
                    Name = "Turkmenistan"
                },
                new Country {
                    Name = "Tuvalu"
                },
                new Country {
                    Name = "Uganda"
                },
                new Country {
                    Name = "Ukraine"
                },
                new Country {
                    Name = "United Arab Emirates"
                },
                new Country {
                    Name = "United Kingdom"
                },
                new Country {
                    Name = "United States of America"
                },
                new Country {
                    Name = "Uruguay"
                },
                new Country {
                    Name = "Uzbekistan"
                },
                new Country {
                    Name = "Vanuatu"
                },
                new Country {
                    Name = "Venezuela"
                },
                new Country {
                    Name = "Vietnam"
                },
                new Country {
                    Name = "Yemen"
                },
                new Country {
                    Name = "Zambia"
                },
                new Country {
                    Name = "Zimbabwe"
                }
            };

            _context.Countries.AddRange(countries);

            await _context.SaveChangesAsync(cancellationToken);
        }