示例#1
0
        public ProjectCategoryDto Convert(ProjectCategory source_object)
        {
            if (source_object == null)
            {
                return(null);
            }

            try
            {
                ProjectCategoryDto project = new ProjectCategoryDto
                {
                    CategoryDescription = source_object.CategoryDescription,
                    CategoryId          = source_object.CategoryId,
                    CategoryName        = source_object.CategoryName,
                    Thumbnail           = string.Format("data:image/jpg;base64,{0}", source_object.Thumbnail),
                };

                return(project);
            }
            catch (Exception ex)
            {
                _loggerRepository.Write(ex);
                return(null);
            }
        }
        public ProjectDto Convert(Project source_object)
        {
            if (source_object == null)
            {
                return(null);
            }

            try
            {
                ProjectDto project = new ProjectDto
                {
                    Active         = source_object.Active,
                    CategoryId     = source_object.CategoryId,
                    Content        = source_object.Content,
                    GitHubUrl      = source_object.GitHubUrl,
                    Images         = source_object.Images.Select(i => string.Format("data:image/jpg;base64,{0}", i.Base64Image)).ToList(),
                    Keywords       = source_object.Keywords,
                    ProjectId      = source_object.ProjectId,
                    Title          = source_object.Title,
                    ThumbnailImage = string.Format("data:image/jpg;base64,{0}", source_object.ThumbnailImage)
                };

                return(project);
            }
            catch (Exception ex)
            {
                _loggerRepository.Write(ex);
                return(null);
            }
        }
示例#3
0
        public List <UserDto> GetAllUsersAsync()
        {
            List <UserDto> users = new List <UserDto>();

            _loggerRepository.Write(LogType.Info, string.Format("GetAllUsersAsync called:"));
            try
            {
                var userManager = _serviceProvider.GetRequiredService <UserManager <ApplicationUser> >();
                var roleManager = _serviceProvider.GetRequiredService <RoleManager <ApplicationRole> >();

                users = userManager.Users.Select(user => new UserDto
                {
                    Id              = user.Id,
                    FirstName       = user.FirstName,
                    LastName        = user.LastName,
                    UserName        = user.UserName,
                    Email           = user.Email,
                    Roles           = GetRolesByEmail(user.Email),
                    IsAdmin         = IsAdmin(user.Email),
                    AvatarImage     = user.AvatarImage,
                    AvatarImageType = user.AvatarImageType
                }).ToList();

                //users = userManager.Users.Include(u => u.UserRoles).Select(user => new User {
                //    Id = user.Id,
                //    FirstName = user.FirstName,
                //    LastName = user.LastName,
                //    UserName = user.UserName,
                //    Email = user.Email,
                //}).ToList();

                //// loop

                //try
                //{
                //    foreach (var user in users)
                //    {

                //        ApplicationUser u = new ApplicationUser { UserName = user.UserName };

                //        var usr = userManager.FindByEmailAsync(user.Email);
                //        //var r = userManager.Users.Select(u=> u.r)
                //        //usr.Rol
                //        //user.Roles = await roleManager.get
                //    }
                //}
                //catch (Exception)
                //{
                //    // empty may not even need this.
                //}
            }
            catch (Exception ex)
            {
                _loggerRepository.Write(ex);
            }
            return(users);
        }
示例#4
0
        public VoteDto AddorUpdateVote(VoteDto incomingVote)
        {
            try
            {
                var post = _applicationDbContext.Posts.Where(p => p.PostId == incomingVote.PostId).FirstOrDefault();

                // did user vote on this post? No
                if (!VoteExists(incomingVote.PostId, incomingVote.UserId))
                {
                    if (incomingVote.UserVote == 1)
                    {
                        post.Likes++;
                    }
                    else if (incomingVote.UserVote == -1)
                    {
                        post.Likes--;
                    }

                    // add new vote
                    _applicationDbContext.Votes.Add(new Vote {
                        PostId = incomingVote.PostId, UserId = incomingVote.UserId, UserVote = incomingVote.UserVote
                    });
                }
                else
                {
                    // get the user vote.
                    var previousVote = _applicationDbContext.Votes.Where(v => v.UserId == incomingVote.UserId).FirstOrDefault();

                    // check it.
                    if (incomingVote.UserVote > previousVote.UserVote)
                    {
                        post.Likes++;
                        post.DisLikes--;
                    }
                    else if (incomingVote.UserVote < previousVote.UserVote)
                    {
                        post.Likes--;
                        post.DisLikes++;// = post.DisLikes++;
                    }

                    previousVote.UserVote = incomingVote.UserVote;
                    // update vote
                    _applicationDbContext.Update(previousVote);
                }
                // update post
                _applicationDbContext.Update(post);
                _applicationDbContext.SaveChanges();
                return(incomingVote);
            }
            catch (Exception ex)
            {
                _loggerRepository.Write(ex);
                return(null);
            }
        }
示例#5
0
        public PostDto Convert(Post sourcePost)
        {
            if (sourcePost == null)
            {
                return(null);
            }

            try
            {
                var     user = _userManager.FindByIdAsync(sourcePost.UserId).Result;
                PostDto post = new PostDto
                {
                    Author       = string.Format("{0} {1}", user.FirstName, user.LastName),
                    Comments     = sourcePost.Comments.Select(c => _converter.Convert(c)).ToList(),
                    Content      = sourcePost.Content,
                    DateCreated  = sourcePost.DateCreated,
                    DateModified = sourcePost.DateModified,
                    DisLikes     = sourcePost.DisLikes,
                    Likes        = sourcePost.Likes,
                    PostId       = sourcePost.PostId,
                    Title        = sourcePost.Title,
                    UserId       = sourcePost.UserId
                };

                return(post);
            }
            catch (Exception ex)
            {
                _loggerRepository.Write(ex);
                return(null);
            }
        }
示例#6
0
        public CommentDto Convert(Comment sourceComment)
        {
            if (sourceComment == null)
            {
                return(null);
            }

            try
            {
                CommentDto comment = new CommentDto
                {
                    Message      = sourceComment.Message,
                    DateCreated  = sourceComment.DateCreated,
                    DateModified = sourceComment.DateModified,
                    DisLikes     = sourceComment.DisLikes,
                    Likes        = sourceComment.Likes,
                    PostId       = sourceComment.PostId,
                    UserId       = sourceComment.UserId,
                    CommentId    = sourceComment.CommentId
                };

                return(comment);
            }
            catch (Exception ex)
            {
                _loggerRepository.Write(ex);
                return(null);
            }
        }
示例#7
0
        public DashboardDto GetDashboard()
        {
            DashboardDto dashboard = new DashboardDto();

            try
            {
                // get errors count
                dashboard.Errors = _applicationDbContext.Logs.Where(e => e.LogType == LogType.Error && e.DateCreated >= DateTime.Now.AddHours(-24)).Count();

                // get users count
                dashboard.Users = _applicationDbContext.Users.Count();

                // get lastest post comments count
                var lastPost     = _applicationDbContext.Posts.LastOrDefault();
                var commentCount = _applicationDbContext.Comments.Where(c => c.PostId == lastPost.PostId).Select(c => c).Count();
                dashboard.Comments = commentCount; //.Comments.Count();

                //get posts count
                dashboard.Posts = _applicationDbContext.Posts.Count();
            }
            catch (Exception ex)
            {
                _loggerRepository.Write(ex);
            }

            return(dashboard);
        }
示例#8
0
        public async Task <IActionResult> CreateProfileImage([FromBody] string userName)
        {
            if (string.IsNullOrEmpty(userName))
            {
                return(StatusCode(500, "userName required."));
            }
            bool didSave = false;
            var  file    = Request.Form.Files[0];
            var  user    = _userManager.Users.Where(c => c.UserName == userName).FirstOrDefault();

            if (user == null)
            {
                return(StatusCode(500, "user not found"));
            }

            // Convert.FromBase64String(user.AvatarImageBas64);

            //BlogImage image = new BlogImage
            //{
            //    Title = file.FileName.Replace(" ", "-")
            //};

            // var blogsRepo = _serviceProvider.GetRequiredService<IBlog>();
            try
            {
                using (var memoryStream = new MemoryStream())
                {
                    await file.CopyToAsync(memoryStream);

                    user.AvatarImage = memoryStream.ToString();
                    _userRepository.UpdateProfile(user);
                    _loggerRepository.Write(LogType.Info, "Added user Image");
                    didSave = true;
                }
            }
            catch (Exception ex)
            {
                _loggerRepository.Write(ex);
            }
            //blogsRepo.

            return(Ok(didSave));
        }
示例#9
0
        public List <RoleDto> GetAllRoles()
        {
            List <RoleDto> roles = new List <RoleDto>();

            try
            {
                roles = _roleManager.Roles.Select(role => new RoleDto
                {
                    Id       = role.Id,
                    RoleName = role.Name
                }).ToList();
            }
            catch (Exception ex)
            {
                _loggerRepository.Write(ex);
            }

            return(roles);
        }
        public ProjectImageDto Convert(ProjectImage source_object)
        {
            if (source_object == null)
            {
                return(null);
            }

            try
            {
                ProjectImageDto projectImage = new ProjectImageDto
                {
                    Base64Image = source_object.Base64Image,
                    ImageId     = source_object.ImageId,
                    ProjectId   = source_object.ProjectId
                };

                return(projectImage);
            }
            catch (Exception ex)
            {
                _loggerRepository.Write(ex);
                return(null);
            }
        }
示例#11
0
        public VoteDto Convert(Vote source_object)
        {
            if (source_object == null)
            {
                return(null);
            }

            try
            {
                VoteDto vote = new VoteDto
                {
                    PostId   = source_object.PostId,
                    UserId   = source_object.UserId,
                    UserVote = source_object.UserVote
                };

                return(vote);
            }
            catch (Exception ex)
            {
                _loggerRepository.Write(ex);
                return(null);
            }
        }
示例#12
0
        public async Task <bool> AddProjectAsync(ProjectDto project)
        {
            try
            {
                _applicationDbContext.Projects.Add(_converter.Convert(project));
                await _applicationDbContext.SaveChangesAsync();

                return(true);
            }
            catch (Exception ex)
            {
                _loggerRepository.Write(ex);
                return(false);
            }
        }
示例#13
0
        public ApplicationUser Convert(UserDto sourceUser)
        {
            if (sourceUser == null)
            {
                return(null);
            }

            try
            {
                List <ApplicationUserRole> roles = new List <ApplicationUserRole>();
                if (sourceUser.Roles.Any())
                {
                    foreach (var item in sourceUser.Roles)
                    {
                        roles.Add(new ApplicationUserRole {
                            Role = new ApplicationRole()
                            {
                                Name = item
                            }
                        });
                    }
                }

                ApplicationUser user = new ApplicationUser
                {
                    AvatarImage     = sourceUser.AvatarImage,
                    AvatarImageType = sourceUser.AvatarImageType,
                    Email           = sourceUser.Email,
                    FirstName       = sourceUser.FirstName,
                    Id        = sourceUser.Id,
                    LastName  = sourceUser.LastName,
                    UserRoles = roles,
                    UserName  = sourceUser.UserName
                };

                return(user);
            }
            catch (Exception ex)
            {
                _loggerRepository.Write(ex);
            }

            return(null);
        }
示例#14
0
        public async Task <UserDto> Login([FromBody] LoginDto model)
        {
            var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, false, false);

            if (result.Succeeded)
            {
                var appUser = _userManager.Users.SingleOrDefault(r => r.Email == model.Email);
                _loggerRepository.Write(LogType.Pass, string.Format("User: {0} Logged In.", appUser.UserName));
                return(await GenerateJwtToken(model.Email, appUser));
            }

            _loggerRepository.Write(LogType.Fail, string.Format("User: {0} Failed to Log In.", model.Email));
            throw new ApplicationException("INVALID_LOGIN_ATTEMPT");
        }
示例#15
0
        public async Task <bool> CreateCommentAsync(CommentDto comment)
        {
            bool didCreate = false;

            try
            {
                //Comment c = new Comment()
                //{
                //    UserId =
                //}
                //comment.User.UserName = await GetCurrentUser();
                // _applicationDbContext.Comments.Add(_commentConverter.Convert(comment));
                await _applicationDbContext.SaveChangesAsync();

                didCreate = true;
            }
            catch (Exception ex)
            {
                _loggerRepository.Write(ex);
            }

            return(didCreate);
        }