Ejemplo n.º 1
0
            public async Task <UserDto> Handle(Query request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var user = await _userManager.FindByEmailAsync(request.Email);

                if (user == null)
                {
                    await _logger.LogActivity(
                        site.Id,
                        ActivityCodeEnum.UserLogin,
                        ActivitySeverityEnum.Medium,
                        ActivityObjectEnum.User,
                        "Login failed. The requested email address or username does not exist.");

                    throw new RestException(HttpStatusCode.Unauthorized);
                }
                user.LastLoginDate = DateTime.UtcNow;

                var result = await _signInManager.CheckPasswordSignInAsync(user, request.Password, false);

                if (result.Succeeded)
                {
                    user.LastLoginDate = DateTime.UtcNow;
                    await _context.SaveChangesAsync();

                    await _logger.LogActivity(
                        site.Id,
                        ActivityCodeEnum.UserLogin,
                        ActivitySeverityEnum.Low,
                        ActivityObjectEnum.User,
                        $"A user with the username \"{user.UserName}\" has been logged in successfully.");

                    var claims = await _userManager.GetClaimsAsync(user);

                    return(new UserDto
                    {
                        DisplayName = user.DisplayName,
                        Token = await _jwtGenerator.CreateTokenAsync(user, claims.ToList()),
                        UserName = user.UserName,
                        Role = user.Role,
                        //ProfilePhotoName = user.CreatedAttachments?.FirstOrDefault(x => x.Type == AttachmentTypeEnum.Photo && x.IsMain)?.FileName
                    });
                }

                await _logger.LogActivity(
                    site.Id,
                    ActivityCodeEnum.UserLogin,
                    ActivitySeverityEnum.Medium,
                    ActivityObjectEnum.User,
                    "Login faild. Password is incorrect!");

                throw new RestException(HttpStatusCode.Unauthorized);
            }
Ejemplo n.º 2
0
            public async Task <DownloadFileDto> Handle(Query request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var file = _context.Attachments.SingleOrDefault(x => x.FileName == request.FileName && x.Type != AttachmentTypeEnum.Photo);

                if (file == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { File = "Not found" });
                }


                var fileMemoryStream = await _uploadAccessor.DownloadFileAsync(file.RelativePath, file.UploadLocation);

                await _logger.LogActivity(
                    site.Id,
                    ActivityCodeEnum.AttachmentDownload,
                    ActivitySeverityEnum.Information,
                    ActivityObjectEnum.Attachemnt,
                    $"The {file.Type} with the name {file.FileName} downloaded");

                return(new DownloadFileDto
                {
                    FileName = file.FileName,
                    Memory = fileMemoryStream,
                    MimeType = file.MimeType
                });
            }
Ejemplo n.º 3
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var comment = await _context.Comments.FindAsync(request.Id);

                if (comment == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { comment = "Not found!" });
                }

                comment.Content = request.Content;

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

                if (success)
                {
                    await _logger.LogActivity(
                        site.Id,
                        ActivityCodeEnum.CommentEdit,
                        ActivitySeverityEnum.Low,
                        ActivityObjectEnum.Comment,
                        $"The comment \"{comment.Content.Substring(0, 30)}\" has been modified.");

                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes!");
            }
Ejemplo n.º 4
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var attachment = await _context.Attachments.SingleOrDefaultAsync(x => x.Id == request.Id);

                if (attachment == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { Attachment = "Not found" });
                }

                var result = await _uploadAccessor.DeleteFileAsync(attachment.RelativePath, attachment.UploadLocation);

                if (result != "ok")
                {
                    throw new Exception("Problem deleting the file!");
                }

                _context.Attachments.Remove(attachment);

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

                if (success)
                {
                    await _logger.LogActivity(
                        attachment.SiteId,
                        ActivityCodeEnum.AttachmentDelete,
                        ActivitySeverityEnum.Low,
                        ActivityObjectEnum.Attachemnt,
                        $"The {attachment.Type} with the name {attachment.FileName} deleted!");

                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes!");
            }
Ejemplo n.º 5
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var optionmeta = await _context.Optionmetas.FindAsync(request.Id);

                if (optionmeta == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { optionmeta = "Not found!" });
                }

                optionmeta.Value = request.Value;

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

                if (success)
                {
                    await _logger.LogActivity(
                        site.Id,
                        ActivityCodeEnum.OptionEdit,
                        ActivitySeverityEnum.High,
                        ActivityObjectEnum.Option,
                        $"The option \"{optionmeta.KeyDescription}\" has been changed.");

                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes!");
            }
Ejemplo n.º 6
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                foreach (var id in request.Ids)
                {
                    var optionmeta = await _context.Optionmetas.FindAsync(id);

                    if (optionmeta == null)
                    {
                        throw new RestException(HttpStatusCode.NotFound, new { optionmeta = "Not found!" });
                    }

                    optionmeta.Value = optionmeta.DefaultValue;
                }

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

                if (success)
                {
                    await _logger.LogActivity(
                        site.Id,
                        ActivityCodeEnum.OptionRestoreDefaultOptions,
                        ActivitySeverityEnum.Critical,
                        ActivityObjectEnum.Option,
                        $"All options has been restored to default.");

                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes!");
            }
Ejemplo n.º 7
0
            public async Task <DownloadFileDto> Handle(Query request, CancellationToken cancellationToken)
            {
                // var refreshToken = _httpContextAccessor.HttpContext.Request.Cookies["refreshToken"];
                // if (refreshToken == null)
                //     throw new RestException(HttpStatusCode.Unauthorized, new { User = "******" });

                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var file = _context.Attachments.SingleOrDefault(x => x.FileName == Path.GetFileNameWithoutExtension(request.FileName));

                if (file == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { File = "Not found" });
                }

                await _logger.LogActivity(
                    site.Id,
                    ActivityCodeEnum.AttachmentDownload,
                    ActivitySeverityEnum.Information,
                    ActivityObjectEnum.Attachemnt,
                    $"The {file.Type} with the name {file.FileName} downloaded");

                return(new DownloadFileDto
                {
                    FileName = file.PublicFileName,
                    Stream = await _uploadAccessor.DownloadFileAsync(file.RelativePath, file.FileSize, file.UploadLocation),
                    MimeType = file.MimeType
                });
            }
Ejemplo n.º 8
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.SingleOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var user = await _userManager.FindByNameAsync(request.UserName);

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

                var result = await _userManager.SetLockoutEndDateAsync(user, DateTimeOffset.UtcNow.AddDays(-1));

                if (result.Succeeded)
                {
                    await _logger.LogActivity(
                        site.Id,
                        ActivityCodeEnum.UserUnlock,
                        ActivitySeverityEnum.High,
                        ActivityObjectEnum.User,
                        $"The user {user.UserName} has been unlocked by Admin.");

                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes!");
            }
Ejemplo n.º 9
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var comment = await _context.Comments.FindAsync(request.Id);

                if (comment == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { comment = "Not found!" });
                }

                var commentTruncatedContent = comment.Content.Substring(0, 30);
                var commentWriterUsername   = comment.CreatedBy.UserName;

                _context.Comments.Remove(comment);

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

                if (success)
                {
                    await _logger.LogActivity(
                        site.Id,
                        ActivityCodeEnum.CommentDelete,
                        ActivitySeverityEnum.Low,
                        ActivityObjectEnum.Comment,
                        $"The comment \"{commentTruncatedContent}\" written by the user \"{commentWriterUsername}\" has been deleted.");

                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes!");
            }
Ejemplo n.º 10
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var taxonomy = await _context.Taxonomies.FindAsync(request.Id);

                if (taxonomy == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { taxonomy = "Not found!" });
                }

                var formerTerm = taxonomy.Term.Name;

                _mapper.Map(request, taxonomy);

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

                if (success)
                {
                    await _logger.LogActivity(
                        site.Id,
                        ActivityCodeEnum.TaxonomyEdit,
                        ActivitySeverityEnum.Medium,
                        ActivityObjectEnum.Taxonomy,
                        $"The taxonomy term with type of ({taxonomy.Type.ToString()}) and with the former name \"{formerTerm}\" changed to \"{taxonomy.Term.Name}\".");

                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes!");
            }
Ejemplo n.º 11
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var taxonomy = await _context.Taxonomies.FindAsync(request.Id);

                if (taxonomy == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { taxonomy = "Not found!" });
                }

                var taxonomyType     = taxonomy.Type.ToString();
                var taxonomyTermName = taxonomy.Term.Name;

                _context.Remove(taxonomy);

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

                if (success)
                {
                    await _logger.LogActivity(
                        site.Id,
                        ActivityCodeEnum.TaxonomyDelete,
                        ActivitySeverityEnum.High,
                        ActivityObjectEnum.Taxonomy,
                        $"The taxonomy with type of \"{taxonomyType}\" and the term name \"{taxonomyTermName}\" has been deleted.");

                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes!");
            }
Ejemplo n.º 12
0
            public async Task <AttachmentDto> Handle(Command request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var fileUploadResult = await _uploadAccessor.AddFileAsync(request.File);

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

                if (!user.CreatedAttachments.Any(x => x.IsMain && x.Type == AttachmentTypeEnum.Photo))
                {
                    fileUploadResult.IsMain = true;
                }

                var userAttachment = _mapper.Map <FileUploadResult, Attachment>(fileUploadResult);

                userAttachment.Site = site;

                user.CreatedAttachments.Add(userAttachment);

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

                if (success)
                {
                    await _logger.LogActivity(
                        site.Id,
                        ActivityCodeEnum.AttachmentAdd,
                        ActivitySeverityEnum.Medium,
                        ActivityObjectEnum.Attachemnt,
                        $"The {userAttachment.Type} with the name {userAttachment.FileName} uploaded");

                    return(_mapper.Map <AttachmentDto>(userAttachment));
                }

                throw new Exception("Problem saving changes!");
            }
Ejemplo n.º 13
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

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

                var photo = user.CreatedAttachments.FirstOrDefault(x => x.Id == request.Id && x.Type == AttachmentTypeEnum.Photo);

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

                var currentMain = user.CreatedAttachments.FirstOrDefault(x => x.Type == AttachmentTypeEnum.Photo && x.IsMain);

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

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

                if (success)
                {
                    await _logger.LogActivity(
                        site.Id,
                        ActivityCodeEnum.AttachmentSetMainPhoto,
                        ActivitySeverityEnum.Information,
                        ActivityObjectEnum.Attachemnt,
                        $"The main photo changed");

                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes!");
            }
Ejemplo n.º 14
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var comment = await _context.Comments.FindAsync(request.Id);

                if (comment == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { comment = "Not found!" });
                }

                if (!comment.Approved)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { comment = "is already unapproved!" });
                }

                comment.Approved = false;

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

                if (success)
                {
                    await _logger.LogActivity(
                        site.Id,
                        ActivityCodeEnum.CommentApprove,
                        ActivitySeverityEnum.Information,
                        ActivityObjectEnum.Comment,
                        $"The comment \"{comment.Content.Substring(0, 30)}\" sent by the user \"{comment.CreatedBy.UserName}\" unapproved");

                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes!");
            }
Ejemplo n.º 15
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var post = await _context.Posts.FindAsync(request.Id);

                if (post == null)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { post = "not found" });
                }

                var postTruncatedContent = post.Title.Substring(0, 30) + "...";
                var postAuthorUsername   = post.CreatedBy.UserName;

                _context.PostAttachments.RemoveRange(post.PostAttachments);
                _context.TaxonomyPosts.RemoveRange(post.TaxonomyPosts);
                _context.Posts.Remove(post);

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

                if (success)
                {
                    await _logger.LogActivity(
                        site.Id,
                        ActivityCodeEnum.CommentDelete,
                        ActivitySeverityEnum.Low,
                        ActivityObjectEnum.Comment,
                        $"The post \"{postTruncatedContent}\" written by the user \"{postAuthorUsername}\" has been deleted.");

                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes!");
            }
Ejemplo n.º 16
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.SingleOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var user = await _userManager.FindByNameAsync(_userAccessor.GetCurrentUsername());

                if (request.NewPassword != request.ConfirmNewPassword)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { ConfirmPassword = "******" });
                }

                var currentPasswordVerfified = await _userManager.CheckPasswordAsync(user, request.CurrentPassword);

                if (!currentPasswordVerfified)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { CurrentPassword = "******" });
                }

                var result = await _userManager.ChangePasswordAsync(user, request.CurrentPassword, request.NewPassword);

                if (result.Succeeded)
                {
                    await _logger.LogActivity(
                        site.Id,
                        ActivityCodeEnum.UserChangePassword,
                        ActivitySeverityEnum.High,
                        ActivityObjectEnum.User,
                        "User account's password has been changed.");

                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes!");
            }
Ejemplo n.º 17
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var option = await _optionAccessor.GetOptionByKeyAsync(KeyEnum.Comment_Blog);

                var comment = _mapper.Map <Comment>(request);

                comment.Site = await _context.Sites.SingleOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                comment.Approved = option.Value == ValueEnum.Comment_Approved;

                _context.Comments.Add(comment);

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

                if (success)
                {
                    await _logger.LogActivity(
                        site.Id,
                        ActivityCodeEnum.CommentCreate,
                        ActivitySeverityEnum.Low,
                        ActivityObjectEnum.Comment,
                        $"The comment \"{comment.Content.Substring(0, 30)}\" written by the user \"{comment.CreatedBy.UserName}\" has been sent.");

                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes!");
            }
 public static void LogSomeData(this IActivityLogger activityLogger, int id, string interesting)
 {
     activityLogger.LogActivity(new ExampleKeySet {
         SpecialExampleId = id
     }, ExampleActionType.Important,
                                new AdditionalData(("Time", DateTime.Now)), "Did you see that interesting thing? {InterestingString}",
                                interesting);
 }
        public static void LogActivity <TActivityEnum>(this IActivityLogger activityLogger, IActivityKeySet keySet,
                                                       TActivityEnum actionType, string message,
                                                       params object[] messageData)
        {
            if (activityLogger == null)
            {
                throw new ArgumentNullException(nameof(activityLogger));
            }

            activityLogger.LogActivity(keySet, actionType, null, message, messageData);
        }
Ejemplo n.º 20
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var post = await _context.Posts.FindAsync(request.Id);

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

                if (request.Title != post.Title)
                {
                    var isTitleExist = await _context.Posts.SingleOrDefaultAsync(x => x.Title == request.Title) != null;

                    if (isTitleExist)
                    {
                        throw new RestException(HttpStatusCode.BadRequest, new { title = "duplicate title is no allowed" });
                    }
                }

                if (request.Slug != post.Slug)
                {
                    var isSlugExist = await _context.Posts.SingleOrDefaultAsync(x => x.Slug == request.Slug) != null;

                    if (isSlugExist)
                    {
                        throw new RestException(HttpStatusCode.BadRequest, new { slug = "duplicate slug is no allowed" });
                    }
                }

                var postHistory = _mapper.Map <PostHistory>(post);

                postHistory.PostId = post.Id;
                _context.PostHistories.Add(postHistory);

                _mapper.Map(request, post);

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

                if (success)
                {
                    await _logger.LogActivity(
                        site.Id,
                        ActivityCodeEnum.PostEdit,
                        ActivitySeverityEnum.Low,
                        ActivityObjectEnum.Post,
                        $"The comment \"{post.Title.Substring(0, 30)}...\" has been modified.");

                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes!");
            }
Ejemplo n.º 21
0
 public static void LogSomeData <TContext>(
     this IActivityLogger <ExampleKeyTypeEnum, TContext> activityLogger,
     int id,
     string interesting)
 {
     activityLogger.LogActivity(
         ExampleActionType.GetOrder,
         ExampleKeyTypeEnum.OrderId,
         id,
         null,
         null,
         "Did you see that interesting thing, the account id was retrieved using the function? {InterestingString}", interesting);
 }
Ejemplo n.º 22
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var posts = new List <Post>();

                foreach (var id in request.Ids)
                {
                    var post = await _context.Posts.Where(x => x.Type == PostTypeEnum.Posts).SingleOrDefaultAsync(x => x.Id == id);

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

                    _context.PostAttachments.RemoveRange(post.PostAttachments);
                    _context.TaxonomyPosts.RemoveRange(post.TaxonomyPosts);
                    foreach (var item in post.Comments)
                    {
                        await RemoveCommentChildren(item.Id);
                    }
                    _context.Comments.RemoveRange(post.Comments);
                    _context.Posts.Remove(post);

                    posts.Add(post);
                }


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

                if (success)
                {
                    foreach (var post in posts)
                    {
                        var postTruncatedContent = post.Title.Length > 30 ? post.Title.Substring(0, 30) + "..." : post.Title;

                        await _logger.LogActivity(
                            site.Id,
                            ActivityCodeEnum.CommentDelete,
                            ActivitySeverityEnum.Low,
                            ActivityObjectEnum.Comment,
                            $"The post \"{postTruncatedContent}\" has been deleted.");
                    }

                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes!");
            }
Ejemplo n.º 23
0
            public async Task <PostsEnvelope> Handle(Query request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                // pagination, sorting and filtering
                var helperTDO = await PostHelpers.PaginateAndFilterAndSort(
                    _context,
                    request.Limit, request.Offset,
                    request.Field, request.Order,
                    request.FilterKey, request.FilterValue,
                    request.StartDate, request.EndDate,
                    request.StartNumber, request.EndNumber
                    );

                var maxAttachmentsNumber = helperTDO.PostCount > 0 ?
                                           _context.Posts
                                           .Where(x => x.Type == PostTypeEnum.Posts)
                                           .OrderByDescending(x => x.PostAttachments.Count)
                                           .First().PostAttachments.Count :
                                           0;
                var maxViewCount = helperTDO.PostCount > 0 ? await
                                   _context.Posts
                                   .Where(x => x.Type == PostTypeEnum.Posts)
                                   .MaxAsync(x => x.ViewCount) :
                                   0;

                var maxComments = helperTDO.PostCount > 0 ?
                                  _context.Posts
                                  .Where(x => x.Type == PostTypeEnum.Posts)
                                  .OrderByDescending(x => x.Comments.Count)
                                  .First().Comments.Count :
                                  0;

                await _logger.LogActivity(
                    site.Id,
                    ActivityCodeEnum.PostList,
                    ActivitySeverityEnum.Information,
                    ActivityObjectEnum.Post,
                    "A list of all posts has been requested and recieved by user");

                return(new PostsEnvelope
                {
                    Posts = _mapper.Map <List <PostDto> >(helperTDO.PostsEnvelope),
                    PostCount = helperTDO.PostCount,
                    MaxAttachmentsNumber = maxAttachmentsNumber,
                    MaxViewCount = maxViewCount,
                    MaxComments = maxComments,
                });
            }
Ejemplo n.º 24
0
            public async Task <List <OptionDto> > Handle(Query request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var options = await _context.Options.ToListAsync();

                await _logger.LogActivity(
                    site.Id,
                    ActivityCodeEnum.OptionList,
                    ActivitySeverityEnum.Medium,
                    ActivityObjectEnum.Option,
                    $"A list of all options has been requested and recieved by user");

                return(_mapper.Map <List <OptionDto> >(options));
            }
Ejemplo n.º 25
0
            public async Task <List <AttachmentDto> > Handle(Query request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var attachments = await _context.Attachments.ToListAsync();

                await _logger.LogActivity(
                    site.Id,
                    ActivityCodeEnum.AttachmentList,
                    ActivitySeverityEnum.Information,
                    ActivityObjectEnum.Attachemnt,
                    "Get list of all attachments");

                return(_mapper.Map <List <AttachmentDto> >(attachments));
            }
Ejemplo n.º 26
0
            public async Task <List <CommentDto> > Handle(Query request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var comments = await _context.Comments.ToListAsync();

                await _logger.LogActivity(
                    site.Id,
                    ActivityCodeEnum.CommentList,
                    ActivitySeverityEnum.Information,
                    ActivityObjectEnum.Comment,
                    $"A list of all comments has been requested and recieved by user");

                return(_mapper.Map <List <CommentDto> >(comments));
            }
Ejemplo n.º 27
0
            public async Task <List <SiteDto> > Handle(Query request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.SingleOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var Sites = await _context.Sites.ToListAsync();

                await _logger.LogActivity(
                    site.Id,
                    ActivityCodeEnum.SiteList,
                    ActivitySeverityEnum.Information,
                    ActivityObjectEnum.Site,
                    "Get a List of all sites infromation.");

                return(_mapper.Map <List <SiteDto> >(Sites));
            }
Ejemplo n.º 28
0
            public async Task <List <TaxonomyDto> > Handle(Query request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FirstOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                var taxonomies = request.TaxonomyType == null ?
                                 await _context.Taxonomies.ToListAsync() :
                                 await _context.Taxonomies.Where(x => x.Type == request.TaxonomyType).ToListAsync();

                await _logger.LogActivity(
                    site.Id,
                    ActivityCodeEnum.TaxonomyList,
                    ActivitySeverityEnum.Information,
                    ActivityObjectEnum.Taxonomy,
                    "A list of all taxonomies has been requested and recieved by user");

                return(_mapper.Map <List <TaxonomyDto> >(taxonomies));
            }
Ejemplo n.º 29
0
            public async Task <UserDto> Handle(Command request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.SingleOrDefaultAsync(x => x.SiteType == SiteTypeEnum.Blog);

                if (await _context.Users.Where(x => x.Email == request.Email).AnyAsync())
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Email = "already exists!" });
                }

                if (await _context.Users.Where(x => x.UserName == request.UserName).AnyAsync())
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Username = "******" });
                }

                var user = new User
                {
                    DisplayName = request.DisplayName,
                    Email       = request.Email,
                    UserName    = request.UserName,
                    Role        = AspianCoreClaimValue.Member,
                    CreatedAt   = DateTime.UtcNow
                };

                var createUserResult = await _userManager.CreateAsync(user, request.Password);

                if (createUserResult.Succeeded)
                {
                    await _logger.LogActivity(
                        site.Id,
                        ActivityCodeEnum.UserRegister,
                        ActivitySeverityEnum.Low,
                        ActivityObjectEnum.User,
                        $"A user with the username \"{user.UserName}\" has been registered successfully.");

                    return(new UserDto
                    {
                        DisplayName = user.DisplayName,
                        Token = await _jwtGenerator.CreateTokenAsync(user, claim : null),
                        UserName = user.UserName,
                        Role = user.Role
                    });
                }

                throw new Exception("Problem creating user!");
            }
Ejemplo n.º 30
0
            public async Task <SiteDto> Handle(Query request, CancellationToken cancellationToken)
            {
                var site = await _context.Sites.FindAsync(request.Id);

                if (site == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { site = "Not found!" });
                }

                await _logger.LogActivity(
                    site.Id,
                    ActivityCodeEnum.SiteDetails,
                    ActivitySeverityEnum.Information,
                    ActivityObjectEnum.Site,
                    $"Details of the site \"{site.SiteType.ToString()}\" has been read.");

                return(_mapper.Map <SiteDto>(site));
            }