示例#1
0
        public ArticlesResponse Articles(Articles articles)
        {
            ArticlesResponse ArticlesResponse = new ArticlesResponse();
            List <Articles>  ListaArticles    = new List <Articles>();

            try
            {
                if (articles == null)
                {
                    ArticlesResponse.articles       = new List <ApiArticles>();
                    ArticlesResponse.total_elements = 0;
                    ArticlesResponse.success        = false;
                    ArticlesResponse.error_code     = 400;
                    ArticlesResponse.error_msg      = "Bad Request";

                    return(ArticlesResponse);
                }

                using (DbContextSuperZapatos superZapatos = new DbContextSuperZapatos(connectionString))
                {
                    superZapatos.Articles.Add(articles);

                    superZapatos.SaveChanges();

                    superZapatos.Stores.ToList();
                    ListaArticles = superZapatos.Articles.ToList();
                }

                ArticlesResponse.articles = (from a in ListaArticles
                                             select new ApiArticles
                {
                    id = a.id,
                    name = a.name,
                    description = a.description,
                    price = a.price,
                    total_in_vault = a.total_in_vault,
                    total_in_shelf = a.total_in_shelf,
                    store_id = a.store_id,
                    store_name = a.store.name
                }
                                             ).ToList();
                ArticlesResponse.total_elements = ListaArticles.Count;
                ArticlesResponse.success        = true;
                ArticlesResponse.error_code     = 0;
                ArticlesResponse.error_msg      = string.Empty;
            }
            catch (Exception ex)
            {
                ArticlesResponse.articles       = new List <ApiArticles>();
                ArticlesResponse.total_elements = 0;
                ArticlesResponse.success        = false;
                ArticlesResponse.error_code     = 500;
                ArticlesResponse.error_msg      = "Server Error";

                log.Error(ex.Message);
            }

            return(ArticlesResponse);
        }
示例#2
0
        public async Task <ArticlesResponse> GetArticles(Pagination pagination = null)
        {
            var articles = await articleStore.GetAsync(pagination);

            var articlesResponse = new ArticlesResponse();

            foreach (var article in articles)
            {
                var articleResponse = mapper.Map <Article, ArticleResponse>(article);
                articlesResponse.Articles.Add(articleResponse);
            }

            return(articlesResponse);
        }
示例#3
0
        public ArticlesResponse GetStoreArticles(int id)
        {
            ArticlesResponse response = new ArticlesResponse()
            {
                Success = true
            };

            try
            {
                var articles = articleService.GetArticles(id);
                response.Articles      = articles;
                response.TotalElements = articles.Count;
            }
            catch (Exception exc)
            {
                response.Success      = false;
                response.ErrorCode    = ((int)ErrorResponse.ServerError).ToString();
                response.ErrorMessage = exc.Message;
            }

            return(response);
        }
示例#4
0
        public async Task <IActionResult> Index([FromQuery] ArticlesQuery query, [FromRoute] int page = 0)
        {
            page = Math.Max(0, page);

            string viewerUserId = this.GetViewerUserId();

            // Temporary for disabling tags feature
            query.Tag = string.Empty;

            ArticlesResponse blogResponse = null;

            if (string.IsNullOrEmpty(query.Q))
            {
                blogResponse = await _articleService.Get(PageSize, page, query.Year, query.Tag, specificUserId : query.UserId,
                                                         viewerUserId : viewerUserId, oldest : query.Oldest).CAF();
            }
            // else
            // {
            //     _logger.LogInformation("Searching for {SearchQuery}", query.Q);
            //     // To minimize spam of searches
            //     await Task.Delay(1000).CAF();
            //
            //     blogResponse = await _articleService.Get(PageSize, query.Q).CAF();
            // }

            if (blogResponse is null)
            {
                return(Redirect("/"));
            }
            if (blogResponse.Articles.Count == 0 && blogResponse.CurrentPage > 0)
            {
                return(Redirect("/"));
            }

            // blogResponse.MostPopularTags = await _tagService.GetTopTags(10).ConfigureAwait(false);

            return(View("Articles", blogResponse));
        }
示例#5
0
        public ArticlesResponse ArticlesStores(string id)
        {
            ArticlesResponse ArticlesResponse = new ArticlesResponse();
            List <Articles>  ListaArticles    = new List <Articles>();
            int idNumero;

            try
            {
                if (id == null)
                {
                    ArticlesResponse.articles       = new List <ApiArticles>();
                    ArticlesResponse.total_elements = 0;
                    ArticlesResponse.success        = false;
                    ArticlesResponse.error_code     = 400;
                    ArticlesResponse.error_msg      = "Bad Request";

                    return(ArticlesResponse);
                }

                bool esNumero = Int32.TryParse(id, out idNumero);

                if (!esNumero)
                {
                    ArticlesResponse.articles       = new List <ApiArticles>();
                    ArticlesResponse.total_elements = 0;
                    ArticlesResponse.success        = false;
                    ArticlesResponse.error_code     = 400;
                    ArticlesResponse.error_msg      = "Bad Request";

                    return(ArticlesResponse);
                }

                using (DbContextSuperZapatos superZapatos = new DbContextSuperZapatos(connectionString))
                {
                    superZapatos.Stores.ToList();
                    ListaArticles = superZapatos.Articles.Where(n => n.store_id == idNumero).ToList();
                }

                if (ListaArticles.Count > 0)
                {
                    ArticlesResponse.articles = (from a in ListaArticles
                                                 select new ApiArticles
                    {
                        id = a.id,
                        name = a.name,
                        description = a.description,
                        price = a.price,
                        total_in_vault = a.total_in_vault,
                        total_in_shelf = a.total_in_shelf,
                        store_id = a.store_id,
                        store_name = a.store.name
                    }
                                                 ).ToList();
                    ArticlesResponse.total_elements = ListaArticles.Count;
                    ArticlesResponse.success        = true;
                    ArticlesResponse.error_code     = 0;
                    ArticlesResponse.error_msg      = string.Empty;
                }
                else
                {
                    ArticlesResponse.articles       = new List <ApiArticles>();
                    ArticlesResponse.total_elements = 0;
                    ArticlesResponse.success        = false;
                    ArticlesResponse.error_code     = 404;
                    ArticlesResponse.error_msg      = "Record not Found";
                }
            }
            catch (Exception ex)
            {
                ArticlesResponse.articles       = new List <ApiArticles>();
                ArticlesResponse.total_elements = 0;
                ArticlesResponse.success        = false;
                ArticlesResponse.error_code     = 500;
                ArticlesResponse.error_msg      = "Server Error";

                log.Error(ex.Message);
            }

            return(ArticlesResponse);
        }
示例#6
0
        public ArticlesResponse Articles(string id, Articles articles)
        {
            ArticlesResponse ArticlesResponse = new ArticlesResponse();
            List <Articles>  ListaArticles    = new List <Articles>();
            int idNumero;

            try
            {
                if (id == null || articles == null)
                {
                    ArticlesResponse.articles       = new List <ApiArticles>();
                    ArticlesResponse.total_elements = 0;
                    ArticlesResponse.success        = false;
                    ArticlesResponse.error_code     = 400;
                    ArticlesResponse.error_msg      = "Bad Request";

                    return(ArticlesResponse);
                }

                bool esNumero = Int32.TryParse(id, out idNumero);

                if (!esNumero)
                {
                    ArticlesResponse.articles       = new List <ApiArticles>();
                    ArticlesResponse.total_elements = 0;
                    ArticlesResponse.success        = false;
                    ArticlesResponse.error_code     = 400;
                    ArticlesResponse.error_msg      = "Bad Request";

                    return(ArticlesResponse);
                }

                if (idNumero != articles.id)
                {
                    ArticlesResponse.articles       = new List <ApiArticles>();
                    ArticlesResponse.total_elements = 0;
                    ArticlesResponse.success        = false;
                    ArticlesResponse.error_code     = 400;
                    ArticlesResponse.error_msg      = "Bad Request";

                    return(ArticlesResponse);
                }

                using (DbContextSuperZapatos superZapatos = new DbContextSuperZapatos(connectionString))
                {
                    Stores storeEdit = superZapatos.Stores.Find(articles.store_id);

                    Articles articlesEdit = superZapatos.Articles.Find(idNumero);
                    if (articlesEdit == null)
                    {
                        ArticlesResponse.articles       = new List <ApiArticles>();
                        ArticlesResponse.total_elements = 0;
                        ArticlesResponse.success        = false;
                        ArticlesResponse.error_code     = 404;
                        ArticlesResponse.error_msg      = "Record not Found";

                        return(ArticlesResponse);
                    }

                    superZapatos.Entry(articlesEdit).State = EntityState.Detached;
                    superZapatos.Entry(articles).State     = EntityState.Modified;

                    superZapatos.SaveChanges();

                    superZapatos.Stores.ToList();
                    ListaArticles = superZapatos.Articles.ToList();
                }

                ArticlesResponse.articles = (from a in ListaArticles
                                             select new ApiArticles
                {
                    id = a.id,
                    name = a.name,
                    description = a.description,
                    price = a.price,
                    total_in_vault = a.total_in_vault,
                    total_in_shelf = a.total_in_shelf,
                    store_id = a.store_id,
                    store_name = a.store.name
                }
                                             ).ToList();
                ArticlesResponse.total_elements = ListaArticles.Count;
                ArticlesResponse.success        = true;
                ArticlesResponse.error_code     = 0;
                ArticlesResponse.error_msg      = string.Empty;
            }
            catch (Exception ex)
            {
                ArticlesResponse.articles       = new List <ApiArticles>();
                ArticlesResponse.total_elements = 0;
                ArticlesResponse.success        = false;
                ArticlesResponse.error_code     = 500;
                ArticlesResponse.error_msg      = "Server Error";

                log.Error(ex.Message);
            }

            return(ArticlesResponse);
        }