Exemplo n.º 1
0
        public async Task <ServiceResult> UpdateAsync(MenuItem model)
        {
            Check.NotNull(model, nameof(model));

            var menuItem = await _context.MenuItems.FirstOrDefaultAsync(x => x.Id == model.Id);

            if (menuItem == null)
            {
                return(ServiceResultFactory.NotFound);
            }

            var changed = false;

            // update text
            if (Utility.IsModified(menuItem.Text, model.Text))
            {
                // Doesn't supported StringComparison.OrdinalIgnoreCase by SQL
                var menuItemTextExist = await _context.MenuItems.AnyAsync(x => model.Text.ToLower() == x.Text.ToLower());

                if (menuItemTextExist)
                {
                    return(ServiceResultFactory.BadRequestResult(nameof(UpdateAsync), $"MenuItem text is already exist."));
                }

                menuItem.Text = model.Text;
                changed       = true;
            }

            // update order
            if (Utility.IsModified(menuItem.Order, model.Order))
            {
                menuItem.Order = model.Order;
                changed        = true;
            }

            // update iconPath
            if (Utility.IsModified(menuItem.IconPath, model.IconPath))
            {
                menuItem.IconPath = model.IconPath;
                changed           = true;
            }

            if (changed == false)
            {
                return(ServiceResultFactory.BadRequestResult(nameof(UpdateAsync), "Nothing to update."));
            }

            _context.MenuItems.Update(menuItem);
            _context.History.Add(ChangeHistory.Create(TableName.MenuItem, ActionType.Modify));

            var result = await _context.SaveAsync(nameof(UpdateAsync));

            result.SetModelIfSuccess(menuItem);

            return(result);
        }
        public async Task <ServiceResult> AddAsync(ContentGroup model)
        {
            Check.NotNull(model, nameof(model));

            _context.ContentGroups.Add(model);
            _context.History.Add(ChangeHistory.Create(model.GroupName, ActionType.Add));

            var result = await _context.SaveAsync(nameof(AddAsync));

            result.SetModelIfSuccess(model);

            return(result);
        }
        public async Task <ServiceResult> UpdateAsync(ContentGroup model)
        {
            Check.NotNull(model, nameof(model));

            var contentGroup = await _context.ContentGroups.FirstOrDefaultAsync(x => x.Id == model.Id);

            if (contentGroup == null)
            {
                return(ServiceResultFactory.NotFound);
            }

            var changed = false;

            // update title
            if (Utility.IsModified(contentGroup.Title, model.Title))
            {
                contentGroup.Title = model.Title;
                changed            = true;
            }

            // update order
            if (Utility.IsModified(contentGroup.Order, model.Order))
            {
                contentGroup.Order = model.Order;
                changed            = true;
            }

            // update groupname
            if (model.GroupName != contentGroup.GroupName)
            {
                contentGroup.GroupName = model.GroupName;
                changed = true;
            }

            if (changed == false)
            {
                return(ServiceResultFactory.BadRequestResult(nameof(UpdateAsync), "Nothing to update."));
            }

            _context.ContentGroups.Update(contentGroup);
            _context.History.Add(ChangeHistory.Create(model.GroupName, ActionType.Modify));

            var result = await _context.SaveAsync(nameof(UpdateAsync));

            result.SetModelIfSuccess(contentGroup);

            return(result);
        }
        public async Task <ServiceResult> AddAsync(InfoObject model)
        {
            Check.NotNull(model, nameof(model));

            var contentGroupExist = await _context.ContentGroups.AnyAsync(x => x.Id == model.ContentGroupId);

            if (contentGroupExist == false)
            {
                return(ServiceResultFactory.BadRequestResult(nameof(model.ContentGroupId), $"ContentGroup not found with id: '{model.ContentGroupId}'."));
            }

            _context.InfoObjects.Add(model);
            _context.History.Add(ChangeHistory.Create(TableName.InfoObject, ActionType.Add));

            var result = await _context.SaveAsync(nameof(AddAsync));

            result.SetModelIfSuccess(model);

            return(result);
        }
Exemplo n.º 5
0
        public async Task <ServiceResult> AddAsync(MenuItem model)
        {
            Check.NotNull(model, nameof(model));
            Check.NotNullOrEmpty(model.Text, nameof(model.Text));

            // Doesn't supported StringComparison.OrdinalIgnoreCase by SQL
            var menuItemExist = await this.ExistAsync(x => model.Text.ToLower() == x.Text.ToLower());

            if (menuItemExist)
            {
                return(ServiceResultFactory.BadRequestResult(nameof(AddAsync), $"MenuItem text already exist."));
            }

            _context.MenuItems.Add(model);
            _context.History.Add(ChangeHistory.Create(TableName.MenuItem, ActionType.Add));

            var result = await _context.SaveAsync(nameof(AddAsync));

            result.SetModelIfSuccess(model);

            return(result);
        }
Exemplo n.º 6
0
        private async Task SeedDatabaseAsync(IDbContext context)
        {
            var users = await this.LoadAsync <List <AppUser> >("Users.json");

            this.ManagePasswords(users);
            await this.SaveToDbAsync(context, users);

            var menuItems = await this.LoadAsync <List <MenuItem> >("MenuItems.json");

            await context.History.AddAsync(ChangeHistory.Create(TableName.MenuItem, ActionType.Add));

            await this.SaveToDbAsync(context, menuItems);

            var discounts = await this.LoadAsync <List <ContentGroup> >("Discounts.json");

            await context.History.AddAsync(ChangeHistory.Create(TableName.Discount, ActionType.Add));

            await this.SaveToDbAsync(context, discounts);

            var delivery = await this.LoadAsync <List <ContentGroup> >("Delivery.json");

            await context.History.AddAsync(ChangeHistory.Create(TableName.Delivery, ActionType.Add));

            await this.SaveToDbAsync(context, delivery);

            var aboutUs = await this.LoadAsync <List <ContentGroup> >("AboutUs.json");

            await context.History.AddAsync(ChangeHistory.Create(TableName.AboutUs, ActionType.Add));

            await this.SaveToDbAsync(context, aboutUs);

            var carts = SeedData.LoadCarts(menuItems.First().Items.First().Details);

            await this.SaveToDbAsync(context, carts);

            var orders = SeedData.LoadOrders(carts);

            await this.SaveToDbAsync(context, orders);
        }
        public async Task <ServiceResult> UpdateAsync(InfoObject model)
        {
            Check.NotNull(model, nameof(model));

            var infoObject = await _context.InfoObjects.FirstOrDefaultAsync(x => x.Id == model.Id);

            if (infoObject == null)
            {
                return(ServiceResultFactory.NotFound);
            }

            var changed = false;

            // update contentGroup
            if (Utility.IsModified(infoObject.ContentGroupId, model.ContentGroupId))
            {
                var contentGroupExist = await _context.ContentGroups.AnyAsync(x => x.Id == model.ContentGroupId);

                if (contentGroupExist == false)
                {
                    return(ServiceResultFactory.BadRequestResult(nameof(model.ContentGroupId), $"ContentGroup not found with id: '{model.ContentGroupId}'."));
                }

                infoObject.ContentGroupId = model.ContentGroupId;
                changed = true;
            }

            // update content
            if (Utility.IsModified(infoObject.Content, model.Content))
            {
                infoObject.Content = model.Content;
                changed            = true;
            }

            // update type
            if (model.Type != infoObject.Type)
            {
                infoObject.Type = model.Type;
                changed         = true;
            }

            // update subOrder
            if (Utility.IsModified(infoObject.SubOrder, model.SubOrder))
            {
                infoObject.SubOrder = model.SubOrder;
                changed             = true;
            }

            if (changed == false)
            {
                return(ServiceResultFactory.BadRequestResult(nameof(UpdateAsync), "Nothing to update."));
            }

            _context.InfoObjects.Update(infoObject);
            _context.History.Add(ChangeHistory.Create(TableName.InfoObject, ActionType.Modify));

            var result = await _context.SaveAsync(nameof(UpdateAsync));

            result.SetModelIfSuccess(infoObject);

            return(result);
        }