コード例 #1
0
        public async Task<UserDto> GetUser(long id)
        {
            using (var context = new PicturesDbContext())
            {
                var user = await context.Users.Include("Logins").FirstOrDefaultAsync(i => i.Id == id);

                return user?.CreateUserDto(user.Logins);
            }
        }
コード例 #2
0
        public async Task<UserDto> GetUserByLoginAsync(string login)
        {
            using (var context = new PicturesDbContext())
            {
                var user = await context.Users.Include(i => i.Logins).FirstOrDefaultAsync(i => i.Login == login);

                    return user?.CreateUserDto(user.Logins);
            }
        }
コード例 #3
0
        public async Task<PictureDto[]> GetPicturesByCampaign(long campaignId)
        {
            using (var context = new PicturesDbContext())
            {
                var query = from campaignPicture in context.CampaignPicture
                    join picture in context.Pictures on campaignPicture.PictureId equals picture.Id
                    where campaignPicture.CampaignId == campaignId
                    select picture;

                return (await query.ToArrayAsync()).Select(Mapper.Map<Picture, PictureDto>).ToArray();
            }
        }
コード例 #4
0
 public async Task<ICollection<StyleDto>> GetStyles()
 {
     using (var context = new PicturesDbContext())
     {
         var query = context.Styles.Select(i => new StyleDto()
         {
             Id = i.Id,
             Name = i.Name
         });
         return await query.ToArrayAsync();
     }
 }
コード例 #5
0
        public UserDto GetUserByExtenalLogin(string externalKey, ExternalLoginType externalLoginType)
        {
            using (var context = new PicturesDbContext())
            {
                var user =
                    context.Logins.FirstOrDefault(
                        i => i.ExternalUserId == externalKey && i.LoginType == externalLoginType);

                if (user == null)
                    return null;

                return user.User.CreateUserDto(user.User.Logins);
            }
        }
コード例 #6
0
        public async Task<UserDto> AddNewUser(NewUserCommandDto newUserCommandDto)
        {
            using (var context = new PicturesDbContext())
            {
                var newUser = new User
                {
                    FirstName = newUserCommandDto.FirstName,
                    LastName = newUserCommandDto.LastName,
                    PhotoRec = newUserCommandDto.PhotoRec,
                    Email = newUserCommandDto.Email
                };

                if (!newUserCommandDto.IsLocalUser)
                {
                    var login = new Login
                    {
                        LoginType = newUserCommandDto.LoginType,
                        ExternalUserId = newUserCommandDto.ExternalLoginId,
                        User = newUser
                    };

                    context.Logins.Add(login);
                }
                else
                {
                    newUser.Login = newUserCommandDto.Login;
                    newUser.Password = newUserCommandDto.Password;
                }

                context.Users.Add(newUser);

                await context.SaveChangesAsync();

                if (newUserCommandDto.IsLocalUser)
                {
                    var user = context.Users.Include(i => i.Logins).First(i => i.Login == newUserCommandDto.Login);
                    return user.CreateUserDto(user.Logins);
                }

                var user1 = context.Logins.Include(i => i.User).First(
                    i =>
                        i.LoginType == newUserCommandDto.LoginType &&
                        i.ExternalUserId == newUserCommandDto.ExternalLoginId).User;
                return user1.CreateUserDto(user1.Logins);
            }
        }
コード例 #7
0
        private static void SaveToDb(IEnumerable<Picture> pictures, int styleId)
        {
            using (var context = new PicturesDbContext())
            {
                var existPictureNames = context.Pictures.Where(i => i.StyleId == styleId).Select(i => i.ImageUrl).ToDictionary(i => i);
                var existPainters = context.Painters.Select(i => new {i.Name, i.Id}).ToDictionary(i => i.Name,i => i.Id);

                foreach (var parsedPicture in pictures)
                {
                    if (!existPictureNames.Keys.Contains(parsedPicture.Url))
                    {
                        long painterId;
                        GuessThePicture.Data.Picture newPicture = new GuessThePicture.Data.Picture()
                        {
                            Name = parsedPicture.Name,
                            ImageUrl = parsedPicture.Url,
                            Guid = Guid.NewGuid().ToString(),
                            StyleId = styleId
                        };
                        if (existPainters.TryGetValue(parsedPicture.Painter, out painterId))
                        {
                            newPicture.PainterId = painterId;
                        }
                        else
                        {
                            var newPainter = new Painter()
                            {
                                Name = parsedPicture.Painter,
                            };

                            existPainters.Add(parsedPicture.Painter,0);
                            newPicture.Painter = newPainter;
                            context.Painters.Add(newPainter);
                        }
                        context.Pictures.Add(newPicture);
                    }
                }

                context.SaveChanges();
            }
        }
コード例 #8
0
 public PictureLogic(UpdatePictureCommand updatePictureCommand)
 {
     this.updatePictureCommand = updatePictureCommand;
     this.context = this.context;
 }
コード例 #9
0
        public async Task Save(SavePictureDto savePictureDto)
        {
            using (var context = new PicturesDbContext())
            {
                if (savePictureDto.Id == 0)
                {
                    context.Pictures.Add(new Picture
                    {
                        Name = savePictureDto.Name,
                        PainterId = savePictureDto.PainterId,
                        StyleId = savePictureDto.StyleId,
                        Year = savePictureDto.Year,
                        Guid = savePictureDto.Guid,
                        ImageUrl = savePictureDto.ImageUrl,
                    });
                }
                else
                {
                    var existedPicture = new Picture {Id = savePictureDto.Id};
                    context.Pictures.Attach(existedPicture);

                    existedPicture.Name = savePictureDto.Name;
                    existedPicture.PainterId = savePictureDto.PainterId;
                    existedPicture.StyleId = savePictureDto.StyleId;
                    existedPicture.Year = savePictureDto.Year;
                    existedPicture.Guid = savePictureDto.Guid;
                    existedPicture.ImageUrl = savePictureDto.ImageUrl;

                    context.Entry(existedPicture).State = EntityState.Modified;
                }

                await context.SaveChangesAsync();
            }
        }
コード例 #10
0
        public async Task<PagingResult<PictureDto>> GetPictures(PictureFilter filter)
        {
            using (var context = new PicturesDbContext())
            {
                var selectQuery = from picture in context.Pictures
                    join style in context.Styles on picture.StyleId equals style.Id
                    join painter in context.Painters on picture.PainterId equals painter.Id
                    join campaignPicture in context.CampaignPicture on picture.Id equals campaignPicture.PictureId into
                    campaignPictureDiF
                    from campaignPicture in campaignPictureDiF.DefaultIfEmpty()
                    join campaign in context.Campaigns on campaignPicture.CampaignId equals campaign.Id into campaignDiF
                    from campaign in campaignDiF.DefaultIfEmpty()
                    select new {picture, painter, style, campaign};

                if (filter.PainterId.HasValue && filter.PainterId.Value > 0)
                {
                    selectQuery = selectQuery.Where(i => i.painter.Id == filter.PainterId.Value);
                }

                if (filter.CampaignId.HasValue && filter.CampaignId.Value > 0)
                {
                    selectQuery = selectQuery.Where(i => i.campaign.Id == filter.CampaignId.Value);
                }


                var query = from row in selectQuery
                            group row.campaign by new {row.picture, row.painter, row.style} into campaignGr
                        select campaignGr;

                var itemsCount = query.Count();

                query = query.OrderBy(i => i.Key.picture.Id);

                if (filter.PageSize.HasValue && filter.PageNumber.HasValue)
                {
                    query = query.Skip(filter.PageSize.Value*filter.PageNumber.Value).Take(filter.PageSize.Value);
                }

                var result = query.ToArray().Select(item => new PictureDto
                        {
                            Id = item.Key.picture.Id,
                            Name = item.Key.picture.Name,
                            Painter =
                                new PainterDto
                                {
                                    Id = item.Key.painter.Id,
                                    Name = item.Key.painter.Name,
                                    BirthYear = item.Key.painter.BirthYear,
                                    DeathYear = item.Key.painter.DeadYear
                                },
                            Year = item.Key.picture.Year,
                            Style = new StyleDto {Id = item.Key.style.Id, Name = item.Key.style.Name},
                            Guid = item.Key.picture.Guid,
                            ImageUrl = item.Key.picture.ImageUrl,
                            Campaigns = item.Where(i => i != null).Select(i => new CampaignDto() {Id = i.Id, Name = i.Name}).ToArray()
                        }).ToArray();

                return new PagingResult<PictureDto>()
                {
                    Items = result,
                    TotalCount = itemsCount
                };
            }
        }
コード例 #11
0
        public async Task<PictureDto> GetPicture(long id)
        {
            using (var context = new PicturesDbContext())
            {
                var picture = await context.Pictures
                    .Include(i => i.Painter)
                    .Include(i => i.Style)
                    .FirstAsync(i => i.Id == id);

                return new PictureDto()
                {
                    Id = picture.Id,
                    Name = picture.Name,
                    Year = picture.Year,
                    Style = Mapper.Map<Style, StyleDto>(picture.Style),
                    ImageUrl = picture.ImageUrl,
                    Guid = picture.Guid,
                    Painter = new PainterDto
                    {
                        Id = picture.Painter.Id,
                        Name = picture.Painter.Name,
                        BirthYear = picture.Painter.BirthYear,
                        DeathYear = picture.Painter.DeadYear
                    }
                };
            }
        }
コード例 #12
0
        public async Task Delete(long id)
        {
            using (var context = new PicturesDbContext())
            {
                var picture = new Picture() {Id = id};

                context.Pictures.Attach(picture);
                context.Entry(picture).State = EntityState.Deleted;

                await context.SaveChangesAsync();
            }
        }
コード例 #13
0
        public async Task<ICollection<UserScoreDto>> GetUsersRank()
        {
            using (var context = new PicturesDbContext())
            {
                var query = from userScore in context.UsersScores
                    join user in context.Users on userScore.UserId equals user.Id
                    join login in context.Logins on user.Id equals login.UserId into userDiE
                    from login in userDiE.DefaultIfEmpty()        
                    group new {userScore, login} by user
                    into userScoreGr
                    select new
                    {
                        user = userScoreGr.Key,
                        userScore =
                            userScoreGr.OrderByDescending(g => g.userScore.GameRightAnswers)
                                .Select(i => i.userScore)
                                .FirstOrDefault(),
                        logins = userScoreGr.Where(i => i.login != null).Select(i => i.login)
                    };

                return
                    (await query.ToArrayAsync()).Select(i => i.user.CreateUserScoreDto(i.logins, i.userScore)).ToArray();
            }
        }
コード例 #14
0
        public async Task<ICollection<UserScoreDto>> GetExternalUserScore(ICollection<string> users)
        {
            using (var context = new PicturesDbContext())
            {
                var query = from login in context.Logins
                    join user in context.Users on login.UserId equals user.Id
                    join score in context.UsersScores on user.Id equals score.UserId
                    where users.Contains(login.ExternalUserId) && login.LoginType == ExternalLoginType.Google
                    group new {login, score} by user
                    into gr
                    select new
                    {
                        user = gr.Key,
                        logins = gr.Select(i => i.login),
                        scores = gr.Select(i => i.score)
                    };

                var result = await query.ToArrayAsync();

                return
                    result.Select(
                        i =>
                            i.user.CreateUserScoreDto(i.logins,
                                i.scores.OrderByDescending(j => j.GameRightAnswers).FirstOrDefault())).ToArray();
            }
        }
コード例 #15
0
        public async Task UpdateUserInfo(long id, string firstName, string lastName)
        {
            using (var context = new PicturesDbContext())
            {
                var userEntity = new User {Id = id};
                var entity = context.Users.Attach(userEntity);

                entity.FirstName = firstName;
                entity.LastName = lastName;

                context.Entry(entity).Property(item => item.FirstName).IsModified = true;
                context.Entry(entity).Property(item => item.LastName).IsModified = true;

                await context.SaveChangesAsync();
            }
        }
コード例 #16
0
        public void UpdateExternalLoginValues(UserDto userDto, string firstName, string lastName, string photoRec)
        {
            using (var context = new PicturesDbContext())
            {
                var userEntity = new User {Id = userDto.Id};
                var entity = context.Users.Attach(userEntity);

                var wasModified = false;

                if (userDto.PhotoRec != photoRec)
                {
                    entity.PhotoRec = photoRec;
                    //context.Entry(entity).Property((item) => item.PhotoRec).IsModified = true;
                    wasModified = true;
                }

                if (userDto.FirstName != firstName)
                {
                    entity.FirstName = photoRec;
                    //context.Entry(entity).Property((item) => item.FirstName).IsModified = true;
                    wasModified = true;
                }

                if (userDto.LastName != lastName)
                {
                    entity.LastName = lastName;
                    //context.Entry(entity).Property((item) => item.LastName).IsModified = true;
                }

                if (wasModified)
                    context.SaveChanges();
            }
        }
コード例 #17
0
        public async Task SaveGameResult(long userId, int gameQuestions, int gameRightAnswers)
        {
            using (var context = new PicturesDbContext())
            {
                var user = await context.Users.FirstOrDefaultAsync(i => i.Id == userId);

                context.UsersScores.Add(new UserScores
                {
                    UserId = user.Id,
                    GameQuestions = gameQuestions,
                    GameRightAnswers = gameRightAnswers
                });

                await context.SaveChangesAsync();
            }
        }
コード例 #18
0
        public async Task<UserScoreDto> GetUserRank(string userLogin)
        {
            using (var context = new PicturesDbContext())
            {
                var query = (from userScore in context.UsersScores
                    join user in context.Users on userScore.UserId equals user.Id
                    where user.Login == userLogin
                    orderby userScore.GameRightAnswers descending
                    select new {user, userScore}).FirstOrDefaultAsync();

                var result = await query;

                return new UserScoreDto
                {
                    UserDto = new UserDto
                    {
                        Login = result.user.Login
                    },
                    QuestionsCount = result.userScore.GameQuestions,
                    RightAnswers = result.userScore.GameRightAnswers
                };
            }
        }