Ejemplo n.º 1
0
        public async Task <IActionResult> AddBom2([FromForm] BomForCreationDto bomForCreationDto)
        {
            var formFile = bomForCreationDto.File;

            if (formFile == null || formFile.Length <= 0)
            {
                return(BadRequest("formfile is empty"));
            }

            if (!Path.GetExtension(formFile.FileName).Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
            {
                return(BadRequest("Not Support file extension"));
            }

            try
            {
                using (var stream = new MemoryStream())
                {
                    await formFile.CopyToAsync(stream);

                    ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;

                    using (var package = new ExcelPackage(stream))
                    {
                        ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
                        var            rowCount  = worksheet.Dimension.Rows;
                        var            colCount  = worksheet.Dimension.Columns;


                        for (int row = 1; row <= rowCount; row++)
                        {
                            var buhnr = worksheet.Cells[row, 1].Value.ToString().Trim();
                            var mnf   = worksheet.Cells[row, 2].Value.ToString().Trim();

                            var component1 = await _repo.GetCompCMnf(mnf);

                            var component2 = await _repo.GetComponentBuhNr(buhnr);

                            if (component1 == null && component2 == null)
                            {
                                GraphQLData componentInfo = await _componentInfo.GetAllComponentInfo(mnf, 1, "USD");

                                if (componentInfo.search.results != null)
                                {
                                    Componentas ComponentasToCreate = new Componentas
                                    {
                                        Mnf            = mnf,
                                        Manufacturer   = componentInfo.search.results[0].part.manufacturer.name,
                                        Detdescription = componentInfo.search.results[0].part.short_description,
                                        BuhNr          = buhnr,
                                        Type           = componentInfo.search.results[0].part.category.name,
                                        Created        = DateTime.Now
                                    };

                                    foreach (var item in componentInfo.search.results[0].part.specs)
                                    {
                                        switch (item.attribute.name)
                                        {
                                        case "Case/Package":
                                            ComponentasToCreate.Size = item.display_value;
                                            break;

                                        case "Resistance":
                                            ComponentasToCreate.Nominal = item.display_value;
                                            break;

                                        case "Capacitance":
                                            ComponentasToCreate.Nominal = item.display_value;
                                            break;

                                        case "Impedance":
                                            ComponentasToCreate.Nominal = item.display_value;
                                            break;

                                        default:
                                            break;
                                        }
                                    }

                                    var createComponent = await _repo.RegisterComponents(ComponentasToCreate);

                                    if (componentInfo.search.results[0].part.images[0].url is string)
                                    {
                                        var uploadParams = new ImageUploadParams()
                                        {
                                            File = new FileDescription(componentInfo.search.results[0].part.images[0].url),
                                        };

                                        var uploadResult = _cloudinary.Upload(uploadParams);
                                        var publicid     = uploadResult.PublicId;
                                        var url          = uploadResult.Uri.ToString();

                                        Photo PhotoToCreate = new Photo
                                        {
                                            PublicId      = publicid,
                                            IsMain        = true,
                                            Url           = uploadResult.Uri.ToString(),
                                            ComponentasId = createComponent.Id
                                        };

                                        await _repo.RegisterPhoto(PhotoToCreate);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (System.Exception)
            {
                throw;
            }
            return(Ok());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> GetComponentOctopartInfo(string Mnf)
        {
            GraphQLData componentInfo = await _componentInfo.GetAllComponentInfo(Mnf, 1, "USD");

            return(Ok(componentInfo));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> ShortRegister([FromForm] ComponentForRegisterShortDto componentForRegisterShortDto)
        {
            var mnfFromRepo = await _repo.GetCompCMnf(componentForRegisterShortDto.Mnf);

            var buhNtFromRepo = await _repo.GetComponentBuhNr(componentForRegisterShortDto.BuhNr);

            if (mnfFromRepo != null || buhNtFromRepo != null)
            {
                return(BadRequest("Komponentas jau užregistruotas"));
            }

            GraphQLData componentInfo = await _componentInfo.GetAllComponentInfo(componentForRegisterShortDto.Mnf, 1, "USD");

            var uploadParams = new ImageUploadParams()
            {
                File = new FileDescription(componentInfo.search.results[0].part.images[0].url),
            };

            var uploadResult = _cloudinary.Upload(uploadParams);
            var publicid     = uploadResult.PublicId;
            var url          = uploadResult.Uri.ToString();

            Componentas ComponentasToCreate = new Componentas
            {
                Mnf            = componentForRegisterShortDto.Mnf,
                Manufacturer   = componentInfo.search.results[0].part.manufacturer.name,
                Detdescription = componentInfo.search.results[0].part.short_description,
                BuhNr          = componentForRegisterShortDto.BuhNr,
                Type           = componentInfo.search.results[0].part.category.name,
                Created        = DateTime.Now
            };

            foreach (var item in componentInfo.search.results[0].part.specs)
            {
                switch (item.attribute.name)
                {
                case "Case/Package":
                    ComponentasToCreate.Size = item.display_value;
                    break;

                case "Resistance":
                    ComponentasToCreate.Nominal = item.display_value;
                    break;

                case "Capacitance":
                    ComponentasToCreate.Nominal = item.display_value;
                    break;

                case "Impedance":
                    ComponentasToCreate.Nominal = item.display_value;
                    break;

                default:
                    break;
                }
            }

            var createComponent = await _repo.RegisterComponents(ComponentasToCreate);


            Photo PhotoToCreate = new Photo
            {
                PublicId      = publicid,
                IsMain        = true,
                Url           = uploadResult.Uri.ToString(),
                ComponentasId = createComponent.Id
            };

            await _repo.RegisterPhoto(PhotoToCreate);

            return(Ok());
        }