Exemple #1
0
        public async Task InsertMerchantCatalog(InsetMerchantCatalogDto InsetMerchantCatalogDto)
        {
            foreach (var MerchantCatalog in InsetMerchantCatalogDto.MerchantCatalog)
            {
                int CategoryID = await _CategoryService.GetCategoryId(MerchantCatalog.CategoryName);

                if (CategoryID > 0)
                {
                    await Repository.InsertAsync(new MerchantCatalog
                    {
                        MerchantID  = InsetMerchantCatalogDto.MerchantID,
                        CategoryID  = CategoryID,
                        Subcategory = MerchantCatalog.Subcategory,
                        Brand       = MerchantCatalog.Brand,
                        Product     = MerchantCatalog.Product,
                    }
                                                 );
                }
            }
        }
        public async Task <IActionResult> insertMerchantCatalog([FromBody] InsetMerchantCatalogDto InsetMerchantCatalogDto)
        {
            try
            {
                int emptyCount = 0;
                foreach (var getMerchantCatalog in InsetMerchantCatalogDto.MerchantCatalog)
                {
                    if (string.IsNullOrEmpty(getMerchantCatalog.CategoryName) || string.IsNullOrEmpty(getMerchantCatalog.Subcategory) || string.IsNullOrEmpty(getMerchantCatalog.Product) || string.IsNullOrEmpty(getMerchantCatalog.Brand))
                    {
                        emptyCount = 1;
                        break;
                    }
                }
                if (emptyCount == 0)
                {
                    int    Checkcount          = 0;
                    string InvalidCategorylist = "";
                    var    distinctList        = from c in InsetMerchantCatalogDto.MerchantCatalog
                                                 group c by new
                    {
                        c.CategoryName,
                        c.Subcategory,
                        c.Brand,
                        c.Product,
                    } into grp
                    select grp.First();

                    int distictcount = distinctList.Count();
                    if (InsetMerchantCatalogDto.Type == "A")
                    {
                        foreach (var getMerchantCatalog in InsetMerchantCatalogDto.MerchantCatalog)
                        {
                            int checkCategory = await _MerchantCategoryService.CheckCategoryDetails(getMerchantCatalog.CategoryName, InsetMerchantCatalogDto.MerchantID);

                            if (checkCategory == 1)
                            {
                                int checkvalue = await _MerchantCatalogService.CheckMerchantCatalog(InsetMerchantCatalogDto.MerchantID, getMerchantCatalog.CategoryName, getMerchantCatalog.Subcategory, getMerchantCatalog.Brand, getMerchantCatalog.Product);

                                if (checkvalue == 0)
                                {
                                    Checkcount++;
                                }
                            }
                            else
                            {
                                InvalidCategorylist = InvalidCategorylist + ',' + getMerchantCatalog.CategoryName;
                            }
                        }
                        if (InsetMerchantCatalogDto.MerchantCatalog.Count == Checkcount && InsetMerchantCatalogDto.MerchantCatalog.Count == distinctList.Count())
                        {
                            await _MerchantCatalogService.InsertMerchantCatalog(InsetMerchantCatalogDto);

                            return(Ok(new GenericResultDto <string> {
                                Result = "Merchant Catalog created successfully"
                            }));
                        }
                        else
                        {
                            if (InsetMerchantCatalogDto.MerchantCatalog.Count != distinctList.Count())
                            {
                                return(BadRequest(new GenericResultDto <string> {
                                    Result = "Cannot have a duplicate row with the same Category, Subcategory, Brand, Product for a Merchant"
                                }));
                            }
                            if (InvalidCategorylist != "")
                            {
                                return(BadRequest(new GenericResultDto <string> {
                                    Result = "Invalid categories " + InvalidCategorylist.TrimStart(',') + ",Please upload with valid categories"
                                }));
                            }
                        }
                    }
                    else if (InsetMerchantCatalogDto.Type == "D")
                    {
                        foreach (var getMerchantCatalog in InsetMerchantCatalogDto.MerchantCatalog)
                        {
                            int checkvalue = await _MerchantCatalogService.CheckMerchantCatalog(InsetMerchantCatalogDto.MerchantID, getMerchantCatalog.CategoryName, getMerchantCatalog.Subcategory, getMerchantCatalog.Brand, getMerchantCatalog.Product);

                            if (checkvalue == 0)
                            {
                                await _MerchantCatalogService.InsertMerchantCatalog(InsetMerchantCatalogDto);

                                return(Ok(new GenericResultDto <string> {
                                    Result = "Merchant Catalog created successfully"
                                }));
                            }
                        }
                    }
                    else if (InsetMerchantCatalogDto.Type == "O")
                    {
                        foreach (var getMerchantCatalog in InsetMerchantCatalogDto.MerchantCatalog)
                        {
                            int checkCategory = await _MerchantCategoryService.CheckCategoryDetails(getMerchantCatalog.CategoryName, InsetMerchantCatalogDto.MerchantID);

                            if (checkCategory == 0)
                            {
                                InvalidCategorylist = InvalidCategorylist + ',' + getMerchantCatalog.CategoryName;
                            }
                        }
                        if (InvalidCategorylist == "" && InsetMerchantCatalogDto.MerchantCatalog.Count == distinctList.Count())
                        {
                            await _MerchantCatalogService.deleteMerchantCatalogbyMerchantId(InsetMerchantCatalogDto.MerchantID);

                            await _MerchantCatalogService.InsertMerchantCatalog(InsetMerchantCatalogDto);

                            return(Ok(new GenericResultDto <string> {
                                Result = "Merchant Catalog created successfully"
                            }));
                        }
                        else
                        {
                            if (InsetMerchantCatalogDto.MerchantCatalog.Count != distinctList.Count())
                            {
                                return(BadRequest(new GenericResultDto <string> {
                                    Result = "Cannot have a duplicate row with the same Category, Subcategory, Brand, Product for a Merchant"
                                }));
                            }
                            if (InvalidCategorylist != "")
                            {
                                return(BadRequest(new GenericResultDto <string> {
                                    Result = "Invalid categories " + InvalidCategorylist.TrimStart(',') + ",Please upload with valid categories"
                                }));
                            }
                        }
                    }

                    return(BadRequest(new GenericResultDto <string> {
                        Result = "Cannot have a duplicate row with the same Category, Subcategory, Brand, Product for a Merchant"
                    }));
                }
                else
                {
                    return(BadRequest(new GenericResultDto <string> {
                        Result = "The file contains empty column values."
                    }));
                }
            }

            catch (Exception err)
            {
                return(BadRequest(new GenericResultDto <string> {
                    Result = err.Message
                }));
            }
        }