Пример #1
0
        public static async Task <ChangeListNameResult> ChangeListname(DBContext c, int id, int userId, string newName)
        {
            var list = await Q.From(ShoppingList.T).Where(x => x.Id.EqV(id)).Where(x => Q.Exists(Q.From(Contributor.T).Where(a => a.ListId.Eq(x.Id), a => a.UserId.EqV(userId), a => a.Permission.GteV(Contributor.Permissions.Admin)).Select(new RawSql("null")))).FirstOrDefault <ShoppingList>(c.Connection);

            if (list == null)
            {
                return new ChangeListNameResult {
                           Success = false, Error = "Insufficient rights"
                }
            }
            ;
            var ctc = ChangeTrackingContext.StartWith(list);

            list.Name = newName;
            var listId = list.Id;

            string action = "ListRename";

            FirebaseCloudMessaging.TopicMessage($"{list.Id}shoppingListTopic", new { userId, listId, list.Name, action });

            await ctc.Commit(c.Connection);

            return(new ChangeListNameResult {
                Success = true, ListId = list.Id, Name = list.Name
            });
        }
Пример #2
0
        public static async Task <AddListItemResult> AddProduct(DBContext c, int listId, int userId, string name, string gtin, int amount, int?order)
        {
            var cont = await Q.From(Contributor.T).Where(a => a.ListId.EqV(listId), a => a.UserId.EqV(userId)).FirstOrDefault <Contributor>(c.Connection);

            if (cont == null)
            {
                return new AddListItemResult {
                           Success = false, Error = "You are not a contributor"
                }
            }
            ;

            if (!order.HasValue)
            {
                order = (int)(await Q.From(ListItem.T).Where(x => x.ListId.EqV(listId)).Select(x => Q.Count(x.Id)).FirstOrDefault <DbFunctionModel>(c.Connection)).Count + 1;
            }

            var li = new ListItem {
                Gtin = gtin, Name = name, Amount = amount, ListId = listId, SortOrder = order.Value, Created = DateTime.Now
            };
            await Q.InsertOne(c.Connection, li);

            //TODO Same name and same gtin <-- WTF?

            string action = "NewItemAdded";

            FirebaseCloudMessaging.TopicMessage($"{listId}shoppingListTopic", new { userId, listId, li.Id, li.Name, li.Gtin, li.Amount, li.SortOrder, action });
            return(new AddListItemResult {
                Success = true, Gtin = li.Gtin, Name = name, ProductId = li.Id
            });
        }
Пример #3
0
        public static async Task <Result> DeleteProduct(DBContext c, int listId, int userId, int productId)
        {
            var cont = await Q.From(Contributor.T).Where(a => a.ListId.EqV(listId), a => a.UserId.EqV(userId)).FirstOrDefault <Contributor>(c.Connection);

            if (cont == null)
            {
                return new Result {
                           Success = false, Error = "You are not a contributor"
                }
            }
            ;
            var p = await Q.From(ListItem.T).Where(x => x.ListId.EqV(listId), x => x.Id.EqV(productId)).FirstOrDefault <ListItem>(c.Connection);// list.Products.FirstOrDefault(x => x.Id == productId);

            if (p == null)
            {
                return new Result {
                           Success = false, Error = "Product was not found"
                }
            }
            ;
            var ctc = ChangeTrackingContext.StartWith(p);

            p.BoughtAmount = p.Amount;
            p.Amount       = 0;
            string action = "ItemDeleted";

            FirebaseCloudMessaging.TopicMessage($"{listId}shoppingListTopic", new { userId, listId, p.Id, action });
            await ctc.Commit(c.Connection);

            return(new Result {
                Success = true
            });
        }
Пример #4
0
 public ChapterManagerController(DiscordBot discordBot, FirebaseCloudMessaging firebaseCloudMessaging, IWebHostEnvironment webHostEnvironment, MangadexUploader mangadexUploader, NaniWebContext naniWebContext,
                                 RedditPoster redditPoster, SettingsKeeper settingsKeeper)
 {
     _discordBot             = discordBot;
     _firebaseCloudMessaging = firebaseCloudMessaging;
     _webHostEnvironment     = webHostEnvironment;
     _mangadexUploader       = mangadexUploader;
     _naniWebContext         = naniWebContext;
     _redditPoster           = redditPoster;
     _settingsKeeper         = settingsKeeper;
 }
Пример #5
0
        public static async Task <Result> ReorderProducts(DBContext c, int listId, int userId, List <int> productIds)
        {
            var products = await Q.From(ListItem.T).InnerJoin(Contributor.T).On((x, y) => x.ListId.Eq(y.ListId))
                           .Where((a, s) => a.ListId.EqV(listId), (a, s) => s.UserId.EqV(userId)).Select((x, y) => new RawSql(x.TableAlias + ".*")).ToList <ListItem>(c.Connection);

            if (products == null)
            {
                return new Result {
                           Success = false, Error = "User is not allowed to access this list"
                }
            }
            ;

            var notFoundIds = new List <int>();
            int hash        = 0;
            var ctc         = new ChangeTrackingContext();

            for (int i = 1; i <= productIds.Count; i++)
            {
                var id      = productIds[i - 1];
                var product = products.FirstOrDefault(x => x.Id == id);
                if (product == null)
                {
                    notFoundIds.Add(id);
                }
                else
                {
                    ctc.Track(product);
                    product.SortOrder = i;
                    hash += product.Id * i;
                }
            }
            await ctc.Commit(c.Connection);

            string action = "Refresh";

            FirebaseCloudMessaging.TopicMessage($"{listId}shoppingListTopic", new { userId, listId, action });

            if (notFoundIds.Count == 0)
            {
                return new HashResult {
                           Success = true, Hash = hash
                }
            }
            ;
            else
            {
                return new DeleteProductsResult {
                           Success = true, Error = "Some Products could not be found in the Database", productIds = notFoundIds
                }
            };
        }
Пример #6
0
        public static async Task <Result> DeleteProducts(DBContext c, int listId, int userId, List <int> productIds)
        {
            var cont = await Q.From(Contributor.T).Where(a => a.ListId.EqV(listId), a => a.UserId.EqV(userId)).FirstOrDefault <Contributor>(c.Connection);

            if (cont == null)
            {
                return new Result {
                           Success = false, Error = "You are not a contributor "
                }
            }
            ;
            var notFoundIds = new List <int>();
            var p           = await Q.From(ListItem.T).Where(x => x.ListId.EqV(listId), x => x.Id.InV(productIds)).ToList <ListItem>(c.Connection);

            var ctc = new ChangeTrackingContext();

            foreach (var product in p)
            {
                ctc.Track(product);

                if (product == null)
                {
                    notFoundIds.Add(product.Id);
                }
                else
                {
                    product.BoughtAmount = product.Amount;
                    product.Amount       = 0;
                }
            }
            await ctc.Commit(c.Connection);

            string action = "Refresh";

            FirebaseCloudMessaging.TopicMessage($"{listId}shoppingListTopic", new { userId, listId, action });
            if (notFoundIds.Count == 0)
            {
                return new Result {
                           Success = true
                }
            }
            ;
            else
            {
                return new DeleteProductsResult {
                           Success = true, Error = "Some Products could not be found in the Database", productIds = notFoundIds
                }
            };
        }
Пример #7
0
        public static async Task <AddContributorResult> AddContributor(DBContext c, int listId, int userId, User contributor)
        {
            var list = await Q.From(ShoppingList.T).Where(x => x.Id.EqV(listId)).Where(x => Q.Exists(Q.From(Contributor.T).Where(a => a.ListId.Eq(x.Id), a => a.UserId.EqV(userId), a => a.Permission.GteV(Contributor.Permissions.Admin)).Select(new RawSql("null")))).FirstOrDefault <ShoppingList>(c.Connection);

            if (list == null)
            {
                return new AddContributorResult {
                           Success = false, Error = "insufficient rights"
                }
            }
            ;

            var existingContributor = await Q.From(Contributor.T).Where(x => x.UserId.EqV(contributor.Id), x => x.ListId.EqV(listId)).FirstOrDefault <Contributor>(c.Connection);

            if (existingContributor != null)
            {
                return new AddContributorResult {
                           Success = false, Error = "user is already contributor"
                }
            }
            ;
            var con = new Contributor
            {
                UserId     = contributor.Id,
                Permission = Contributor.Permissions.User,
                ListId     = listId
            };
            await Q.InsertOne(c.Connection, con);

            List <ListItem> items;

            items = await Q.From(ListItem.T).Where(li => li.ListId.EqV(listId), li => li.Amount.NeqV(0)).ToList <ListItem>(c.Connection);


            FirebaseCloudMessaging.TopicMessage($"{contributor.Username}userTopic", "You were added to the list " + list.Name, null);
            FirebaseCloudMessaging.TopicMessage($"{contributor.Username}userTopic", new { userId, listId, list.Name, items });

            return(new AddContributorResult {
                Success = true, Id = contributor.Id, Name = contributor.Username
            });
        }
        private void Initialize(IServiceAccountCredentials credentials)
        {
            var creds = credentials ?? throw new ArgumentNullException(nameof(credentials));

            Auth = new FirebaseAuth(creds, Configuration);

            if (FirebaseServiceAccess.Database == (Configuration.RequestedAccess & FirebaseServiceAccess.Database))
            {
                Database = new FirebaseDatabase(creds, Configuration);
            }

            if (FirebaseServiceAccess.Storage == (Configuration.RequestedAccess & FirebaseServiceAccess.Storage))
            {
                Storage = new FirebaseStorage(creds, Configuration);
            }

            if (FirebaseServiceAccess.CloudMessaging == (Configuration.RequestedAccess & FirebaseServiceAccess.CloudMessaging))
            {
                CloudMessaging = new FirebaseCloudMessaging(creds, Configuration);
            }
        }
Пример #9
0
        public static async Task <Result> DeleteList(DBContext c, int listId, int userId)
        {
            var cont = await Q.From(Contributor.T).Where(a => a.ListId.EqV(listId), a => a.UserId.EqV(userId)).FirstOrDefault <Contributor>(c.Connection);

            if (cont == null)
            {
                return new Result {
                           Success = false, Error = "The List could not be found or you are not a contributor."
                }
            }
            ;

            if (cont.Permission == Contributor.Permissions.Owner)
            {
                using (var tx = c.Connection.BeginTransaction())
                {
                    await Q.DeleteFrom(ListItem.T).Where(x => x.ListId.EqV(listId)).Execute(c.Connection, tx);

                    await Q.DeleteFrom(Contributor.T).Where(x => x.ListId.EqV(listId)).Execute(c.Connection, tx);

                    await Q.DeleteFrom(ShoppingList.T).Where(x => x.Id.EqV(listId)).Execute(c.Connection, tx);

                    tx.Commit();
                }
            }
            else
            {
                //Q.Create(Contributor.T).Column(x => x.Permission).Type("asd");
                //await Q.DeleteFrom(Contributor.T).Where(x => x.ListId.EqV(listId), x => x.UserId.EqV(userId)).Execute(c.Connection);
            }

            FirebaseCloudMessaging.TopicMessage($"{listId}shoppingListTopic", new { userId, listId });
            return(new Result {
                Success = true
            });
        }
Пример #10
0
        public static async Task <Result> ChangeProducts(DBContext c, int listId, int userId, List <int> productIds, List <int> changes)
        {
            if (productIds.Count != changes.Count)
            {
                return new Result {
                           Success = false, Error = "Length of product ids doesn't match with length of change list"
                }
            }
            ;

            var products = await Q.From(ListItem.T).InnerJoin(Contributor.T).On((x, y) => x.ListId.Eq(y.ListId))
                           .Where((a, s) => a.ListId.EqV(listId), (a, s) => s.UserId.EqV(userId)).ToList <ListItem>(c.Connection);

            if (products == null)
            {
                return new Result {
                           Success = false, Error = "User is not allowed to access this list"
                }
            }
            ;

            //var shoppinglist = await c.ShoppingLists.Include(x => x.Contributors).Include(x => x.Products).FirstOrDefaultAsync(x => x.Id == listId);

            var notFoundIds = new List <int>();
            int hash        = 0;
            var ctc         = new ChangeTrackingContext();

            for (int i = 0; i < productIds.Count; i++)
            {
                var id      = productIds[i];
                var change  = changes[i];
                var product = products.FirstOrDefault(x => x.Id == id);
                if (product == null)
                {
                    notFoundIds.Add(id);
                }
                else
                {
                    ctc.Track(product);
                    if (product.Amount + change <= 0 || change == 0)
                    {
                        product.Amount = 0;
                    }
                    else
                    {
                        product.Amount += change;
                    }
                    hash += product.Amount + product.Id;
                }
            }
            await ctc.Commit(c.Connection);

            string action = "Refresh";

            FirebaseCloudMessaging.TopicMessage($"{listId}shoppingListTopic", new { userId, listId, action });

            if (notFoundIds.Count == 0)
            {
                return new HashResult {
                           Success = true, Hash = hash
                }
            }
            ;
            else
            {
                return new DeleteProductsResult {
                           Success = true, Error = "Some Products could not be found in the Database", productIds = notFoundIds
                }
            };
        }
Пример #11
0
        public static async Task <ChangeListItemResult> ChangeProduct(DBContext c, int listId, int userId, int productId, int change, int?order, string newName)
        {
            //var cont = await Q.From(Contributor.T).Where(x => x.ListId.EqV(listId), x => x.UserId.EqV(userId)).FirstOrDefault<Contributor>(c.Connection);
            //if (cont == null)
            //    return new ChangeListItemResult { Success = false, Error = "User is not allowed to access this list" };
            var query = Q.From(ListItem.T)
                        .InnerJoin(Contributor.T)
                        .On((l, c) => l.ListId.Eq(c.ListId))
                        .Where((l, c) => l.Id.EqV(productId), (l, c) => l.ListId.EqV(listId), (l, c) => c.UserId.EqV(userId))
                        .Select((l, c) => new RawSql(l.TableAlias + ".*"));
            var product = await query
                          .FirstOrDefault <ListItem>(c.Connection);

            if (product == null)
            {
                return new ChangeListItemResult {
                           Success = false, Error = "Product not found"
                }
            }
            ;

            string action;
            var    ctc = new ChangeTrackingContext();

            ctc.Track(product);
            if (change != 0)
            {
                if (product.Amount + change <= 0 || change == 0)
                {
                    product.BoughtAmount = product.Amount;
                    product.Amount       = 0;
                    action = "ItemDeleted";
                    FirebaseCloudMessaging.TopicMessage($"{product.ListId}shoppingListTopic", new { userId, product.ListId, product.Id, action });
                }
                else
                {
                    product.BoughtAmount = 0;
                    product.Amount      += change;
                    if (product.Amount - change == 0)
                    {
                        action = "NewItemAdded";
                        FirebaseCloudMessaging.TopicMessage($"{product.ListId}shoppingListTopic", new { userId, product.ListId, product.Id, product.Amount, product.Name, product.SortOrder, action });
                    }
                    else
                    {
                        action = "ItemChanged";
                        FirebaseCloudMessaging.TopicMessage($"{product.ListId}shoppingListTopic", new { userId, product.ListId, product.Id, product.Amount, product.SortOrder, action });
                    }
                }
            }
            if (!string.IsNullOrWhiteSpace(newName))
            {
                product.Name = newName;

                action = "ItemRenamed";
                FirebaseCloudMessaging.TopicMessage($"{product.ListId}shoppingListTopic", new { userId, product.ListId, product.Id, product.Name, action });
            }
            if (order.HasValue)
            {
                product.SortOrder = order.Value;
                action            = "OrderChanged";
                FirebaseCloudMessaging.TopicMessage($"{product.ListId}shoppingListTopic", new { userId, product.ListId, product.Id, product.SortOrder, action });
            }
            await ctc.Commit(c.Connection);

            return(new ChangeListItemResult
            {
                Success = true,
                Id = product.Id,
                Name = product.Name,
                Amount = product.Amount,
                Order = product.SortOrder,
                Changed = product.Changed,
                ListId = product.ListId
            });
        }
Пример #12
0
 public SubscriptionController(FirebaseCloudMessaging firebaseCloudMessaging)
 {
     _firebaseCloudMessaging = firebaseCloudMessaging;
 }