示例#1
0
        public static async Task <Result> ChangePassword(int id, string o, string n, bool passwordReset = false)
        {
            using var c = new DBContext(await NsslEnvironment.OpenConnectionAsync(), false);
            var k = await Q.From(User.T).Where(x => x.Id.EqV(id)).FirstOrDefault <User>(c.Connection);// c.Users.FirstOrDefault(x => x.Id == id);

            if (k.PasswordHash.SequenceEqual(Salting(o, k.Salt)) || passwordReset)
            {
                var ctc = ChangeTrackingContext.StartWith(k);

                k.PasswordHash = Salting(n, k.Salt);
                await ctc.Commit(c.Connection);

                c.Connection.Close();
            }
            else
            {
                c.Connection.Close();
                return(new Result {
                    Success = false, Error = "old password was incorrect"
                });
            }

            return(new Result {
                Success = true
            });
        }
示例#2
0
        public static async Task <Result> ChangeRights(DBContext c, int listId, int requesterId, int changeUserId)
        {
            //var list = await Q.From(ShoppingList.T).Where(x => x.UserId.EqV(requesterId), x => x.Id.EqV(listId)).FirstOrDefault<ShoppingList>(c.Connection);

            var contributors = await Q.From(Contributor.T).Where(x => x.ListId.EqV(listId), x => x.UserId.InV(new[] { requesterId, changeUserId })).ToList <Contributor>(c.Connection);// list.Contributors.FirstOrDefault(x => x.UserId == requesterId);

            var requester  = contributors.FirstOrDefault(x => x.UserId == requesterId);
            var changeUser = contributors.FirstOrDefault(x => x.UserId == changeUserId);

            if (requester == null || changeUser == null)
            {
                return new Result {
                           Error = "Either you or the other user was not part of the list"
                }
            }
            ;

            if (!Contributor.Permissions.CanChange(requester.Permission, changeUser.Permission))
            {
                return new Result {
                           Error = "You are not an admin or you wanted to demote the owner"
                }
            }
            ;

            ChangeTrackingContext ctc = new ChangeTrackingContext();

            ctc.Track(changeUser);
            changeUser.Permission = Contributor.Permissions.IsAdmin(changeUser.Permission) ? Contributor.Permissions.User : Contributor.Permissions.Admin;
            await ctc.Commit(c.Connection);

            return(new Result {
                Success = true
            });
        }
示例#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 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
            });
        }
示例#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
                }
            };
        }
        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.fcm.TopicMessage($"{listId}shoppingListTopic",
                                                    new { userId, listId, action },
                                                    priority: Firebase.Priority.normal);
            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
 /// <summary>
 /// 初始化一个<see cref="DomainObjectBase{TObject}"/>类型的实例
 /// </summary>
 protected DomainObjectBase()
 {
     _validationContext     = new ValidationContext <TObject>(AssignableType(this));
     _descriptionContext    = new DescriptionContext();
     _changeTrackingContext = new ChangeTrackingContext();
 }
示例#8
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
                }
            };
        }
示例#9
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
            });
        }
        public static async Task <ChangeListItemResult> ChangeProduct(DBContext c, int listId, int userId, int productId, int change, 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 product = await Q.From(ListItem.T).Where(x => x.ListId.EqV(listId), x => x.Id.EqV(productId)).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.fcm.TopicMessage($"{listId}shoppingListTopic",
                                                            new { userId, listId, product.Id, action },
                                                            priority: Firebase.Priority.normal);
                }
                else
                {
                    product.BoughtAmount = 0;
                    product.Amount      += change;
                    if (product.Amount - change == 0)
                    {
                        action = "NewItemAdded";
                        FirebaseCloudMessaging.fcm.TopicMessage($"{listId}shoppingListTopic",
                                                                new { userId, listId, product.Id, product.Amount, product.Name, action },
                                                                priority: Firebase.Priority.normal);
                    }
                    else
                    {
                        action = "ItemChanged";
                        FirebaseCloudMessaging.fcm.TopicMessage($"{listId}shoppingListTopic",
                                                                new { userId, listId, product.Id, product.Amount, action },
                                                                priority: Firebase.Priority.normal);
                    }
                }
            }
            if (!string.IsNullOrWhiteSpace(newName))
            {
                product.Name = newName;

                action = "ItemRenamed";
                FirebaseCloudMessaging.fcm.TopicMessage($"{listId}shoppingListTopic",
                                                        new { userId, listId, product.Id, product.Name, action },
                                                        priority: Firebase.Priority.normal);
            }
            await ctc.Commit(c.Connection);

            return(new ChangeListItemResult
            {
                Success = true,
                Id = product.Id,
                Name = product.Name,
                Amount = product.Amount,
                Changed = product.Changed,
                ListId = listId
            });
        }