public async Task <User> FindUserWithSerials(int userId)
        {
            var userCacheDictionary   = new Dictionary <int, User>();
            var serialCacheDictionary = new Dictionary <int, Serial>();

            return((await QueryJoin <User, Serial>(new SqlBuilder()
                                                   .Select()
                                                   .From(Table).As("u")
                                                   .LeftJoin("u.id", new JoinIndex(RepositoryDictionary.FindTable(typeof(ISerialRepository)), "userid"),
                                                             "s")
                                                   .Where("k.id").Equals.Append(userId)
                                                   .Build(), (user, serial) =>
            {
                User userEntry;
                Serial serialEntry;

                if (!userCacheDictionary.TryGetValue(user.Id, out userEntry))
                {
                    userEntry = user;
                    userCacheDictionary.Add(userEntry.Id, userEntry);
                }

                if (serial != null && !serialCacheDictionary.TryGetValue(serial.Id, out serialEntry))
                {
                    serialEntry = serial;
                    userEntry.Serials.Add(serialEntry);

                    serialCacheDictionary.Add(serialEntry.Id, serialEntry);
                }

                return userEntry;
            })).FirstOrDefault());
        }
Exemple #2
0
        public async Task <IEnumerable <Purchase> > GetPurchasesWithUsersByAdmin(IAdminPurchaseFiltersParams filtersParams)
        {
            var query = new SqlBuilder()
                        .Select("p.id", "p.name", "p.cost", "p.date", "p.account", "k.id", "k.username")
                        .From(Table)
                        .As("p")
                        .LeftJoin("p.account",
                                  new JoinIndex(RepositoryDictionary.FindTable(typeof(IUserRepository)), "id"), "k")
                        .Where(new SqlBuilder().If(
                                   new SqlBuilder().Append($"'{filtersParams.Username}'").IsNotNull.Build().Query,
                                   new SqlBuilder().Append("k.username").Like($"%{filtersParams.Username}%").Build()
                                   .Query, "k.username").Build().Query)
                        .OrderBy("p.date", (OrderByType)filtersParams.SortType)
                        .Build().Query;

            return(await QueryJoin <Purchase, User>(
                       new SqlBuilder()
                       .Select("p.id", "p.name", "p.cost", "p.date", "p.account", "k.id", "k.username")
                       .From(Table)
                       .As("p")
                       .LeftJoin("p.account",
                                 new JoinIndex(RepositoryDictionary.FindTable(typeof(IUserRepository)), "id"), "k")
                       .Where(new SqlBuilder().If(
                                  new SqlBuilder().Append($"'{filtersParams.Username}'").IsNotNull.Build().Query,
                                  new SqlBuilder().Append("k.username").Like($"%{filtersParams.Username}%").Build()
                                  .Query, "k.username").Build().Query)
                       .OrderBy("p.date", (OrderByType)filtersParams.SortType)
                       .Build(), (purchase, user) =>
            {
                purchase.SetAccount(user);
                return purchase;
            }
                       ));
        }
        public virtual GenericRepository <T> GetRepository <T>() where T : BaseData
        {
            if (!RepositoryDictionary.ContainsKey(typeof(T)))
            {
                RepositoryDictionary.Add(typeof(T), new GenericRepository <T>(this.Context));
            }

            return((GenericRepository <T>)RepositoryDictionary[typeof(T)]);
        }
 public async Task <User> FindUserByUsername(string username)
 => await QueryFirst(new SqlBuilder()
                     .Select().Append(",").Append("b.id").As("BanId")
                     .From(Table).As("k")
                     .LeftJoin("k.id", new JoinIndex(RepositoryDictionary.FindTable(typeof(IRepository <Ban>)), "account"),
                               "b")
                     .Where("k.username")
                     .Equals
                     .Append($"'{username}'")
                     .Build());
Exemple #5
0
 public async Task <IEnumerable <Faction> > GetTopFactionsByBankBalance(int top = Constants.TopStatsLimit)
 => await Query(new SqlBuilder()
                .Select().Append(",")
                .Open
                .SelectCount()
                .From(RepositoryDictionary.FindTable(typeof(ICharacterRepository))).As("p")
                .Where("p.faction_id").Equals.Append("f.id").Close.As("WorkersCount")
                .From(Table).As("f")
                .OrderBy("f.bankbalance", OrderByType.Descending)
                .Limit(top)
                .Build());
Exemple #6
0
 public async Task <PremiumFile> GetFileWithOrder(string premiumFileId)
 => (await QueryJoin <PremiumFile, Order>(new SqlBuilder()
                                          .Select()
                                          .From(Table).As("pf")
                                          .Join("pf.orderId", new JoinIndex(RepositoryDictionary.FindTable(typeof(IOrderRepository)), "id"),
                                                "o")
                                          .Where("pf.id").Equals.Append($"'{premiumFileId}'")
                                          .Build(), (file, order) =>
 {
     file.SetOrder(order);
     return(file);
 })).FirstOrDefault();
Exemple #7
0
        public IActionResult GetDictionary()
        {
            RepositoryDictionary RP = new RepositoryDictionary();

            try
            {
                List <Dictionaries> List = RP.getDictionary();
                return(Ok(List));
            }
            catch (Exception e)
            {
                return(BadRequest(e));
            }
        }
Exemple #8
0
        public static TRepository Get <TRepository>(this RepositoryDictionary repositories) where TRepository : class
        {
            if (repositories == null)
            {
                return(null);
            }

            if (!repositories.TryGetValue(typeof(TRepository), out var repository))
            {
                throw new InvalidOperationException($"'{typeof(TRepository).Name}' type has not been created.");
            }

            return((TRepository)repository);
        }
Exemple #9
0
 public async Task <IEnumerable <Purchase> > GetPurchasesWithUsers(DateSortType sortType,
                                                                   int userId)
 => await QueryJoin <Purchase, User>(
     new SqlBuilder()
     .Select("p.id", "p.name", "p.cost", "p.date", "p.account", "k.id", "k.username")
     .From(Table)
     .As("p")
     .LeftJoin("p.account",
               new JoinIndex(RepositoryDictionary.FindTable(typeof(IUserRepository)), "id"), "k")
     .Where("p.account").Equals.Append(userId)
     .OrderBy("p.date", (OrderByType)sortType)
     .Build(), (purchase, user) =>
 {
     purchase.SetAccount(user);
     return(purchase);
 }
     );
Exemple #10
0
        public static SqlQuery BuildIsPermittedQuery(int userId, params RoleType[] roleTypes)
        {
            string rolesConditions = string.Empty;

            roleTypes.ToList()
            .ForEach(r => rolesConditions += RoleTypeSmartEnum.FromValue((int)r).IsPermitted().Query);

            return(new SqlBuilder()
                   .Exists(new SqlBuilder()
                           .Select()
                           .From(RepositoryDictionary.FindTable(typeof(IUserRepository)))
                           .Where("id")
                           .Equals
                           .Append(userId)
                           .And
                           .Open
                           .Append(rolesConditions)
                           .Build())
                   .Close
                   .As("IsPermitted")
                   .Build());
        }
Exemple #11
0
        public async Task <Report> GetReport(string reportId)
        {
            var cacheReportDictionary           = new Dictionary <string, Report>();
            var cacheReportCommentsDictionary   = new Dictionary <string, ReportComment>();
            var cacheReportSubscriberDictionary = new Dictionary <(string, int), ReportSubscriber>();
            var cacheReportImageDictionary      = new Dictionary <string, ReportImage>();

            var siema = new SqlBuilder()
                        .Select()
                        .From(Table).As("r")
                        .LeftJoin("r.id",
                                  new(RepositoryDictionary.FindTable(typeof(IRepository <ReportComment>)), "reportId"))
                        .LeftJoin("r.id",
                                  new(RepositoryDictionary.FindTable(typeof(IRepository <ReportSubscriber>)), "reportId"),
                                  "rs")
                        .LeftJoin("r.id",
                                  new(RepositoryDictionary.FindTable(typeof(IRepository <ReportImage>)), "reportId"),
                                  "ri")
                        .LeftJoin("r.id",
                                  new(RepositoryDictionary.FindTable(typeof(IRepository <BugReport>)), "reportId"), "br")
                        .LeftJoin("r.id",
                                  new(RepositoryDictionary.FindTable(typeof(IRepository <PenaltyReport>)), "reportId"), "pr")
                        .LeftJoin("r.id",
                                  new(RepositoryDictionary.FindTable(typeof(IRepository <UserReport>)), "reportId"), "ur")
                        .Where("r.id").Equals.Append($"'{reportId}'")
                        .Build();

            return((await QueryJoin <Report, ReportComment, ReportSubscriber, ReportImage, BugReport, PenaltyReport,
                                     UserReport>(
                        siema, (report, reportComment, reportSubscriber, reportImage, bugReport, penaltyReport,
                                userReport) =>
            {
                Report reportEntry;
                ReportComment reportCommentEntry;
                ReportSubscriber reportSubscriberEntry;
                ReportImage reportImageEntry;

                if (!cacheReportDictionary.TryGetValue(report.Id, out reportEntry))
                {
                    reportEntry = report;
                    cacheReportDictionary.Add(reportEntry.Id, reportEntry);
                }

                if (reportComment != null &&
                    !cacheReportCommentsDictionary.TryGetValue(reportComment.Id, out reportCommentEntry))
                {
                    reportCommentEntry = reportComment;
                    reportEntry.ReportComments.Add(reportCommentEntry);

                    cacheReportCommentsDictionary.Add(reportCommentEntry.Id, reportCommentEntry);
                }

                if (reportSubscriber != null &&
                    !cacheReportSubscriberDictionary.TryGetValue(
                        (reportSubscriber.ReportId, reportSubscriber.UserId),
                        out reportSubscriberEntry))
                {
                    reportSubscriberEntry = reportSubscriber;
                    reportEntry.ReportSubscribers.Add(reportSubscriberEntry);

                    cacheReportSubscriberDictionary.Add(
                        (reportSubscriberEntry.ReportId, reportSubscriberEntry.UserId),
                        reportSubscriberEntry);
                }

                if (reportImage != null &&
                    !cacheReportImageDictionary.TryGetValue(reportImage.Id, out reportImageEntry))
                {
                    reportImageEntry = reportImage;
                    reportEntry.ReportImages.Add(reportImageEntry);

                    cacheReportImageDictionary.Add(reportImageEntry.Id, reportImageEntry);
                }

                if (bugReport != null)
                {
                    reportEntry.SetBugReport(bugReport);
                }
                if (penaltyReport != null)
                {
                    reportEntry.SetPenaltyReport(penaltyReport);
                }
                if (userReport != null)
                {
                    reportEntry.SetUserReport(userReport);
                }

                return reportEntry;
            }, splitOn: "id, id, id, reportId, reportId, reportId")).FirstOrDefault());
        public async Task <User> GetUserWithCharacters(int userId)
        {
            var cacheUserDictionary      = new Dictionary <int, User>();
            var cacheCharacterDictionary = new Dictionary <int, Character>();
            var cacheEstateDictionary    = new Dictionary <int, Estate>();
            var cacheVehicleDictionary   = new Dictionary <int, Vehicle>();
            var cacheSerialDictionary    = new Dictionary <int, Serial>();

            return((await QueryJoin <User, Serial, Character, Faction, Vehicle, VehiclesShop, Estate>(
                        new SqlBuilder()
                        .Select()
                        .From(Table).As("k")
                        .LeftJoin("k.id",
                                  new JoinIndex(RepositoryDictionary.FindTable(typeof(ISerialRepository)), "userid"), "s")
                        .LeftJoin("k.id",
                                  new JoinIndex(RepositoryDictionary.FindTable(typeof(ICharacterRepository)),
                                                "account"),
                                  "p")
                        .LeftJoin("p.faction_id",
                                  new JoinIndex(RepositoryDictionary.FindTable(typeof(IFactionRepository)), "id"),
                                  "f")
                        .LeftJoin("p.id",
                                  new JoinIndex(RepositoryDictionary.FindTable(typeof(IRepository <Vehicle>)),
                                                "owner"),
                                  "pj")
                        .LeftJoin("pj.model",
                                  new JoinIndex(RepositoryDictionary.FindTable(typeof(IRepository <VehiclesShop>)), "vehmtamodel"),
                                  "ps")
                        .LeftJoin("p.id",
                                  new JoinIndex(RepositoryDictionary.FindTable(typeof(IRepository <Estate>)), "owner"), "n")
                        .Where("k.id")
                        .Equals
                        .Append(userId)
                        .Build(), (user, serial, character, faction, vehicle, vehicleShop, estate) =>
            {
                User userEntry;
                Character characterEntry = default;
                Vehicle vehicleEntry;
                Estate estateEntry;
                Serial serialEntry;

                if (!cacheUserDictionary.TryGetValue(user.Id, out userEntry))
                {
                    userEntry = user;
                    cacheUserDictionary.Add(userEntry.Id, userEntry);
                }

                if (serial != null && !cacheSerialDictionary.TryGetValue(serial.Id, out serialEntry))
                {
                    serialEntry = serial;
                    userEntry.Serials.Add(serialEntry);

                    cacheSerialDictionary.Add(serialEntry.Id, serialEntry);
                }

                if (character != null &&
                    !cacheCharacterDictionary.TryGetValue(character.Id, out characterEntry))
                {
                    characterEntry = character;
                    characterEntry.SetFaction(faction);

                    cacheCharacterDictionary.Add(characterEntry.Id, characterEntry);
                }

                if (estate != null && character != null &&
                    !cacheEstateDictionary.TryGetValue(estate.Id, out estateEntry))
                {
                    estateEntry = estate;
                    characterEntry.Estates.Add(estateEntry);

                    cacheEstateDictionary.Add(estateEntry.Id, estateEntry);
                }

                if (vehicle != null && character != null &&
                    !cacheVehicleDictionary.TryGetValue(vehicle.Id, out vehicleEntry))
                {
                    vehicleEntry = vehicle;
                    vehicleEntry.SetVehiclesShop(vehicleShop);
                    characterEntry.Vehicles.Add(vehicleEntry);

                    cacheVehicleDictionary.Add(vehicleEntry.Id, vehicleEntry);
                }

                if (characterEntry != null)
                {
                    userEntry.Characters.Add(characterEntry);
                }

                return userEntry;
            }
                        )).FirstOrDefault());
        }