public async Task <GetCommentDTO> CreateComment(NewCommentDTO newComment, string userName) { try { User user = await _userManager.FindByNameAsync(userName); Comment comment = Mapper.Map <Comment>(newComment); comment.CreatedUserId = user.Id; comment.CreatedDate = DateTime.UtcNow; comment.UpdatedUserId = user.Id; comment.UpdatedDate = DateTime.UtcNow; if (newComment.ActionFor == ActionFor.Post) { comment.PostId = newComment.ActionForId; } _repository.Insert(comment); await _unitOfWork.SaveChangesAsync(); var resultComment = Mapper.Map <GetCommentDTO>(comment); resultComment.CreatedUserAvatar = user.Avatar; resultComment.CreatedUserName = user.UserName; return(resultComment); } catch (Exception ex) { throw new Exception(ResponseCodeString.CommentCreate_Error, ex.InnerException); } }
public async Task <GetLikeDTO> ToggleLike(NewLikeDTO newLike, string userName) { try { User user = await _userManager.FindByNameAsync(userName); IEnumerable <Like> likes = _repository.Queryable().Where(c => c.PostId == newLike.ActionForId && c.CreatedUserId == user.Id); Like like = null; if (likes.Count() > 0) { like = likes.FirstOrDefault(); _repository.Delete(like); } else { like = Mapper.Map <Like>(newLike); like.CreatedUserId = user.Id; like.CreatedDate = DateTime.UtcNow; like.UpdatedUserId = user.Id; like.UpdatedDate = DateTime.UtcNow; if (newLike.ActionFor == ActionFor.Post) { like.PostId = newLike.ActionForId; } _repository.Insert(like); } await _unitOfWork.SaveChangesAsync(); return(Mapper.Map <GetLikeDTO>(like)); } catch (Exception ex) { throw new Exception(ResponseCodeString.LikeCreate_Error, ex.InnerException); } }
public async Task <GetPlayerDTO> CreateOrUpdatePlayer(NewPlayerDTO newPlayer, string userName) { try { User user = await _userManager.FindByNameAsync(userName); Player player = Mapper.Map <Player>(newPlayer); if (player.Id == 0) { player.IsActived = false; player.CreatedUserId = user.Id; player.CreatedDate = DateTime.UtcNow; player.UpdatedUserId = user.Id; player.UpdatedDate = DateTime.UtcNow; _repository.Insert(player); } else { player.UpdatedUserId = user.Id; player.UpdatedDate = DateTime.UtcNow; _repository.Update(player); } var tagRepository = _repository.GetRepository <Tag>(); var tag = tagRepository.Queryable().FirstOrDefault(t => t.Slug == player.Slug); if (tag == null) { tag = new Tag { Avatar = player.Avatar, FullName = player.FullName, ShortName = player.ShortName, NickName = player.NickName, Slug = player.Slug, TagType = TagType.Player }; tagRepository.Insert(tag); } else { tag.Avatar = player.Avatar; tag.FullName = player.FullName; tag.ShortName = player.ShortName; tag.NickName = player.NickName; tag.Slug = player.Slug; tag.TagType = TagType.Player; tagRepository.Update(tag); } await _unitOfWork.SaveChangesAsync(); return(Mapper.Map <GetPlayerDTO>(player)); } catch (Exception ex) { throw new Exception(ResponseCodeString.PlayerCreate_Error, ex.InnerException); } }
public async Task <GetTeamDTO> CreateOrUpdateTeam(NewTeamDTO newTeam, string userName) { try { User user = await _userManager.FindByNameAsync(userName); Team team = Mapper.Map <Team>(newTeam); if (team.Id == 0) { team.IsActived = false; team.CreatedUserId = user.Id; team.CreatedDate = DateTime.UtcNow; team.UpdatedUserId = user.Id; team.UpdatedDate = DateTime.UtcNow; _repository.Insert(team); } else { team.UpdatedUserId = user.Id; team.UpdatedDate = DateTime.UtcNow; _repository.Update(team); } var tagRepository = _repository.GetRepository <Tag>(); var tag = tagRepository.Queryable().FirstOrDefault(t => t.Slug == team.Slug); if (tag == null) { tag = new Tag { Avatar = team.Logo, FullName = team.FullName, ShortName = team.ShortName, NickName = team.NickName, Slug = team.Slug, TagType = TagType.Team }; tagRepository.Insert(tag); } else { tag.Avatar = team.Logo; tag.FullName = team.FullName; tag.ShortName = team.ShortName; tag.NickName = team.NickName; tag.Slug = team.Slug; tag.TagType = TagType.Team; tagRepository.Update(tag); } await _unitOfWork.SaveChangesAsync(); return(Mapper.Map <GetTeamDTO>(team)); } catch (Exception ex) { throw new Exception(ResponseCodeString.TeamCreate_Error, ex.InnerException); } }
public async Task <User> GetUserByName(string userName) { var user = await _userManager.FindByNameAsync(userName); return(user); }
public async Task <IEnumerable <GetGagDTO> > GetGags(int skip, int take, string userFilter, GagFilter filter, string slug) { bool isAdminUser = ClaimsPrincipal.Current.Claims.Any(c => c.Value == "Admin") || ClaimsPrincipal.Current.Claims.Any(c => c.Value == "SuperAdmin"); IQueryable <Post> gags = _repository.Queryable(); gags = gags.Include(p => p.Images) .Include(p => p.Videos) .Include(p => p.Likes.Select(l => l.CreatedUser)) .Include(p => p.Comments) .Include(p => p.Tags) .Include(p => p.CreatedUser) .Where(p => p.PostType == PostType.GAG && p.IsActived); if (!string.IsNullOrEmpty(userFilter)) { User postUser = await _userManager.FindByNameAsync(userFilter); gags = gags.Where(p => p.CreatedUserId == postUser.Id); } if (filter == GagFilter.Tag) { gags = gags.Where(p => p.Tags.Count(t => t.Slug == slug) > 0 || string.IsNullOrEmpty(slug)); var tagRepository = _repository.GetRepository <Tag>(); var tag = await tagRepository.Queryable().FirstOrDefaultAsync(t => t.Slug == slug); if (tag != null) { tag.SearchCount++; } tagRepository.Update(tag); await _unitOfWork.SaveChangesAsync(); } else if (filter == GagFilter.Search) { gags = gags.Where(p => p.Title.Contains(slug) || p.Description.Contains(slug) || p.Tags.Count(t => t.Slug.Contains(slug)) > 0 || string.IsNullOrEmpty(slug)); } else { switch (filter) { case GagFilter.Image: gags = gags.Where(p => p.Images.Count(i => i.ImageType == ImageType.Static) > 0); break; case GagFilter.Gif: gags = gags.Where(p => p.Images.Count(i => i.ImageType == ImageType.Gif) > 0); break; case GagFilter.Video: gags = gags.Where(p => p.Videos.Count() > 0); break; default: break; } if (!string.IsNullOrEmpty(slug)) { var indexOfSlug = _repository.Queryable().OrderByDescending(p => p.PublishedDate).Where(p => (p.IsActived == !isAdminUser) || isAdminUser).ToList().FindIndex(g => g.Slug == slug); skip = indexOfSlug == -1 ? skip : indexOfSlug + skip; } } gags = gags.OrderByDescending(p => p.PublishedDate) .Skip(skip) .Take(take); var result = await gags.ToListAsync(); return(result.Select(Mapper.Map <GetGagDTO>)); }
public async Task <GetLeagueDTO> CreateOrUpdateLeague(NewLeagueDTO newLeague, string userName) { try { User user = await _userManager.FindByNameAsync(userName); League league = Mapper.Map <League>(newLeague); if (league.Id == 0) { league.IsActived = false; league.CreatedUserId = user.Id; league.CreatedDate = DateTime.UtcNow; league.UpdatedUserId = user.Id; league.UpdatedDate = DateTime.UtcNow; _repository.Insert(league); } else { league.UpdatedUserId = user.Id; league.UpdatedDate = DateTime.UtcNow; _repository.Update(league); } var tagRepository = _repository.GetRepository <Tag>(); var tag = tagRepository.Queryable().FirstOrDefault(t => t.Slug == league.Slug); if (tag == null) { tag = new Tag { Avatar = league.Logo, FullName = league.FullName, ShortName = league.ShortName, NickName = league.NickName, Slug = league.Slug, TagType = TagType.League }; tagRepository.Insert(tag); } else { tag.Avatar = league.Logo; tag.FullName = league.FullName; tag.ShortName = league.ShortName; tag.NickName = league.NickName; tag.Slug = league.Slug; tag.TagType = TagType.League; tagRepository.Update(tag); } await _unitOfWork.SaveChangesAsync(); return(Mapper.Map <GetLeagueDTO>(league)); } catch (Exception ex) { throw new Exception(ResponseCodeString.LeagueCreate_Error, ex.InnerException); } }