Example #1
0
        public async Task <ActionResult <MenuItem> > CreateProductAsync([FromBody] MenuItem menu)
        {
            if (menu is null)
            {
                return(BadRequest());
            }

            var item = new MenuItem
            {
                Id         = menu.Id,
                Cid        = menu.Cid,
                Tag        = menu.Tag,
                Name       = menu.Name,
                IsDelete   = menu.IsDelete,
                SortCode   = menu.SortCode,
                CreateUser = menu.CreateUser,
                CreateTime = DateTime.Now
            };

            await _articleContext.MenuItems.AddAsync(item);

            await _articleContext.SaveChangesAsync();

            //return CreatedAtAction(nameof(ItemByIdAsync), new { id = item.Id }, null);
            return(item);
        }
        public async Task <ActionResult <Article> > Post([FromBody] Article value)
        {
            context.articles.Add(value);
            await context.SaveChangesAsync();

            return(CreatedAtAction("get", value));
        }
Example #3
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Title,Content")] Article article)
        {
            if (id != article.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(article);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ArticleExists(article.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(article));
        }
Example #4
0
        public async Task <IActionResult> PutArticle(int id, Article article)
        {
            if (id != article.Id)
            {
                return(BadRequest());
            }

            _context.Entry(article).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ArticleExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #5
0
        public async Task <IActionResult> Create(Article article)
        {
            db.Articles.Add(article);
            await db.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
Example #6
0
        public async Task <IActionResult> Create([Bind("Id,Title,Description")] Article article)
        {
            if (ModelState.IsValid)
            {
                _context.Add(article);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(article));
        }
Example #7
0
        public async Task <IActionResult> Create([Bind("MenuId,MenuName")] Menu menu)
        {
            if (ModelState.IsValid)
            {
                _context.Add(menu);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(menu));
        }
        public async Task <IActionResult> AddToFavorite(int id)
        {
            Article article = db.Articles.FirstOrDefault(g => g.Id == id);
            User    user    = dbUsers.Users.FirstOrDefault(g => g.UserName == User.Identity.Name);

            if (article != null)
            {
                article.UserId = user.Id;
            }
            await db.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
Example #9
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var article = await _articleContext.Articles.FindAsync(request.Id);

                var user = await _articleContext.Users.SingleOrDefaultAsync(x => x.UserName == _contextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.Name));

                var like = await _articleContext.UserArticles.SingleOrDefaultAsync(x => x.ArticleId == article.Id && x.UserId == user.Id);

                if (like != null)
                {
                    throw new Exception("beğendin");
                }

                like = new UserArticle {
                    User    = user,
                    Article = article,
                    isHost  = false
                };

                _articleContext.UserArticles.Add(like);
                var success = await _articleContext.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }
                throw new Exception("--");
            }
Example #10
0
        [HttpPost("add")] // api/article/add
        public async Task <ActionResult <Article> > PostArticle(Article item)
        {
            articleContext.ArticleItems.Add(item);
            await articleContext.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetID), new { id = item.Id }, item));
        }
Example #11
0
        public async Task <IActionResult> About(ArticleModel art)
        {
            await article.AddAsync(art);

            await article.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Example #12
0
        public async Task <ActionResult> UpdateProductAsync([FromBody] ArticleItem productToUpdate)
        {
            var catalogItem = await _articleContext.ArticleItems.SingleOrDefaultAsync(i => i.Id == productToUpdate.Id);

            if (catalogItem == null)
            {
                return(NotFound(new { Message = $"Item with id {productToUpdate.Id} not found." }));
            }

            var oldContent = catalogItem.Content;
            var raiseProductPriceChangedEvent = oldContent != productToUpdate.Content;

            // Update current product
            //catalogItem = productToUpdate;
            catalogItem.Title      = productToUpdate.Title;
            catalogItem.Tag        = productToUpdate.Tag;
            catalogItem.Content    = productToUpdate.Content;
            catalogItem.UpdateUser = productToUpdate.UpdateUser;
            catalogItem.UpdateTime = productToUpdate.UpdateTime;

            _articleContext.ArticleItems.Update(catalogItem);

            await _articleContext.SaveChangesAsync();

            if (raiseProductPriceChangedEvent) // Save product's data and publish integration event through the Event Bus if price has changed
            {
                //Create Integration Event to be published through the Event Bus
                //var priceChangedEvent = new ProductPriceChangedIntegrationEvent(catalogItem.Id, productToUpdate.Price, oldPrice);

                // Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction
                //await _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(priceChangedEvent);

                // Publish through the Event Bus and mark the saved event as published
                //await _catalogIntegrationEventService.PublishThroughEventBusAsync(priceChangedEvent);
            }
            else // Just save the updated product because the Product's Price hasn't changed.
            {
            }


            return(CreatedAtAction(nameof(ItemByIdAsync), new { id = productToUpdate.Id }, null));
        }
Example #13
0
        private async Task <bool> Save()
        {
            try
            {
                await dbContext.SaveChangesAsync();

                return(true);
            }
            catch
            {
                // TODO: Log Exceptions
                return(false);
            }
        }
Example #14
0
 static async Task saveItems(XmlNodeList items)
 {
     using (var db = new ArticleContext()) {
         foreach (XmlNode item in items)
         {
             var summary = new Summary(item);
             if (!db.Summaries.Any(s => s.Guid == summary.Guid))
             {
                 await db.Summaries.AddAsync(summary);
             }
         }
         await db.SaveChangesAsync();
     }
 }
        public async Task <IActionResult> Create([Bind("Id,Title,Content")] Article article)
        {
            if (ModelState.IsValid)
            {
/*                string userId = _userManager.GetUserId(HttpContext.User);
 *              User user = await _userManager.FindByIdAsync(userId);
 *              article.User = user;*/
                _context.Add(article);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(article));
        }
Example #16
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var article = await _articleContext.Articles.FindAsync(request.Id);

                if (article == null)
                {
                    throw new Exception("Makale Bulunamadı");
                }
                _articleContext.Remove(article);
                var success = await _articleContext.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }
                throw new Exception("silinemedi");
            }
Example #17
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                foreach (var item in request.File)
                {
                    if (item.Length > 0)
                    {
                        using (var stream = new MemoryStream())
                        {
                            await item.CopyToAsync(stream);

                            request.Article.ArticleImage = stream.ToArray();
                        }
                    }
                }

                request.Article.Date = DateTime.Now.ToString();

                _articleContext.Articles.Add(request.Article);

                var user = await _articleContext.Users.SingleOrDefaultAsync(x => x.UserName == _contextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.Name));

                var like = new UserArticle {
                    User    = user,
                    Article = request.Article,
                    isHost  = true
                };

                _articleContext.UserArticles.Add(like);

                var success = await _articleContext.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }
                throw new Exception("eklenemedi");
            }