Example #1
0
        public async Task <ActionResult> Edit(int id, BrandViewModel brand)
        {
            /*if (brand.Id != id) // ako neshto gyrmi iztrij ili promeni if
             * {
             *  return NotFound();
             * }
             */
            brand.Id = id;
            try
            {
                BrandsClient brandsClient = new BrandsClient();

                var brandDto = new BrandDto
                {
                    Id                  = brand.Id,
                    BrandName           = brand.BrandName,
                    ManufacturerCountry = brand.ManufacturerCountry,
                    ProductClass        = brand.ProductClass,
                    Rating              = brand.Rating
                };

                await brandsClient.UpdateAsync(brandDto);

                await brandsClient.CloseAsync();

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
Example #2
0
        public async Task <ActionResult> Create(BrandViewModel brand)
        {
            try
            {
                BrandsClient brandsClient = new BrandsClient();

                var brandDto = new BrandDto
                {
                    Id                  = brand.Id,
                    BrandName           = brand.BrandName,
                    ManufacturerCountry = brand.ManufacturerCountry,
                    ProductClass        = brand.ProductClass,
                    Rating              = brand.Rating
                };

                await brandsClient.CreateAsync(brandDto);

                await brandsClient.CloseAsync();

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
Example #3
0
        public bool DeleteBrand(BrandDto brandDto, out string message)
        {
            try
            {
                using (PosRiContext dbContext = new PosRiContext())
                {
                    var brand = dbContext.Brands.Find(brandDto.Id);

                    if (brand == null)
                    {
                        message = "Marca no encontrada.";
                        return(false);
                    }

                    brand.IsActive = false;

                    dbContext.Entry(brand).State = EntityState.Modified;

                    if (dbContext.SaveChanges() > 0)
                    {
                        message = "";
                        return(true);
                    }

                    message = "No se pudo eliminar la marca.";
                    return(false);
                }
            }
            catch (Exception e)
            {
                message = e.Message;
                return(false);
            }
        }
Example #4
0
        public BrandDto AddBrand(BrandDto brandDto, out string message)
        {
            try
            {
                using (PosRiContext dbContext = new PosRiContext())
                {
                    message = "";

                    Brand brand = new Brand
                    {
                        Name     = brandDto.Name,
                        IsActive = true
                    };

                    dbContext.Entry(brand).State = EntityState.Added;
                    dbContext.Brands.Add(brand);

                    if (dbContext.SaveChanges() > 0)
                    {
                        brandDto.Id = brand.Id;
                        return(brandDto);
                    }

                    message = "No se pudo agregar la marca.";
                    return(null);
                }
            }
            catch (Exception e)
            {
                message = e.Message;
                return(null);
            }
        }
Example #5
0
        public async Task <IActionResult> Post([FromBody] BrandCategoryCreateModel brandCategoryCreateModel)
        {
            CategoryDto categoryDto = await _brandCategoryService.GetCategory(brandCategoryCreateModel.Category);

            if (categoryDto == null)
            {
                return(NotFound("No such category"));
            }

            BrandDto brandDto = await _brandCategoryService.GetBrand(brandCategoryCreateModel.Brand);

            if (brandDto == null)
            {
                return(NotFound("No such brand"));
            }

            if (await _brandCategoryService.BrandCategoryExists(brandDto.BrandId, categoryDto.CategoryId))
            {
                return(Conflict($"This relation already exists"));
            }

            BrandCategoryDto newBrandCategoryDto = await _brandCategoryService.CreateRelation(brandDto.BrandId, categoryDto.CategoryId);

            BrandCategoryWebModel newBrandCategoryModel = _mapper.Map <BrandCategoryWebModel>(newBrandCategoryDto);

            return(CreatedAtAction(nameof(Get), new { id = newBrandCategoryModel.BrandCategoryId }, newBrandCategoryModel));
        }
Example #6
0
        public BrandDto UpdateBrand(BrandDto brandDto, out string message)
        {
            try
            {
                using (PosRiContext dbContext = new PosRiContext())
                {
                    message = "";
                    var brand = dbContext.Brands.Find(brandDto.Id);

                    if (brand == null)
                    {
                        message = "Marca no encontrada.";
                        return(null);
                    }

                    brand.Name = brandDto.Name;

                    dbContext.Entry(brand).State = EntityState.Modified;

                    if (dbContext.SaveChanges() > 0)
                    {
                        return(brandDto);
                    }

                    message = "No se pudo actualizar la marca.";
                    return(null);
                }
            }
            catch (Exception e)
            {
                message = e.Message;
                return(null);
            }
        }
        public List <BrandDto> GetBrands()
        {
            var brands = brandRepository.GetAll();

            var brandDtos = new List <BrandDto>();

            foreach (var brand in brands)
            {
                var brandDto = new BrandDto
                {
                    Id      = brand.Id,
                    Name    = brand.Name,
                    LogoUrl = brand.LogoUrl
                };

                brandDtos.Add(brandDto);
            }

            return(brandDtos);

            //return brands.Select(x => new BrandDto
            //{
            //  Id = x.Id,
            //  Name = x.Name,
            //  LogoUrl = x.LogoUrl
            //}).ToList();
        }
Example #8
0
        public IActionResult UpdateBrand(int id, BrandDto brand)
        {
            if (id != brand.Id)
            {
                return(BadRequest());
            }

            try
            {
                _brandService.Modify(new Brand {
                    Id = brand.Id, Name = brand.Name
                });
            }
            catch (DbUpdateConcurrencyException)
            {
                return(NotFound());
            }
            catch (DbUpdateException ex)
            {
                if (ex?.InnerException?.Message != null)
                {
                    return(BadRequest(ex?.InnerException?.Message));
                }
                else
                {
                    return(BadRequest(ex?.Message));
                }
            }

            return(NoContent());
        }
        public async Task <BrandDto> CreateBrand(BrandDto brandDto)
        {
            var brandToCreate = _mapper.Map <Brand>(brandDto);
            var createdBrand  = await CreateAndReturn(brandToCreate);

            return(_mapper.Map <BrandDto>(createdBrand));
        }
        public async Task <BrandDto> UpdateBrandProperties(BrandDto brandDto)
        {
            var brandForUpdate = _mapper.Map <Brand>(brandDto);
            var updatedBrand   = await UpdateAndReturn(brandForUpdate);

            return(_mapper.Map <BrandDto>(updatedBrand));
        }
Example #11
0
        public async Task <IActionResult> PutBrand(int id, BrandDto brand)
        {
            if (id != brand.Id)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var brandDbModel = _brandMapper.ToDbModel(brand);

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

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

            return(NoContent());
        }
Example #12
0
        public IActionResult UpdateBrand(BrandDto brandDto)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                _brandsService.Update(brandDto);

                var integrationEventData = JsonConvert.SerializeObject(new
                {
                    id        = brandDto.Id,
                    brandName = brandDto.BrandName
                });
                PublishToMessageQueue("brands.update", integrationEventData);

                _logger.LogInformation($"Brand updated, id {brandDto.Id}");

                return(NoContent());
            }
            catch (DbQueryResultNullException e)
            {
                return(NotFound(e.Message));
            }
        }
Example #13
0
        public async Task <IActionResult> CreateBrand(BrandDto brandDto)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                if (brandDto.Id != 0)
                {
                    return(BadRequest("The Id should be empty"));
                }

                var createdBrand = await _brandsService.CreateAsync(brandDto);

                var integrationEventData = JsonConvert.SerializeObject(new
                {
                    id        = createdBrand.Id,
                    brandName = createdBrand.BrandName
                });
                PublishToMessageQueue("brands.add", integrationEventData);

                _logger.LogInformation($"Brand added, id {createdBrand.Id}");

                //Fetch the brand from data source
                return(CreatedAtRoute("GetBrandById", new { id = createdBrand.Id }, createdBrand));
            }
            catch (DbQueryResultNullException e)
            {
                return(NotFound(e.Message));
            }
        }
Example #14
0
        public async Task <IActionResult> PutBrandDto(int id, BrandDto brandDto)
        {
            if (id != brandDto.BrandId)
            {
                return(BadRequest());
            }
            var brand = _context.Brand.FirstOrDefault(x => x.BrandId == brandDto.BrandId);

            brand.BrandName             = brandDto.BrandName;
            _context.Entry(brand).State = EntityState.Modified;

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

            return(NoContent());
        }
Example #15
0
 public void AddBrand(BrandDto brand)
 {
     _repo.Insert(new Brand()
     {
         Name = brand.Name, Id = brand.Id
     });
 }
Example #16
0
        public async Task <IActionResult> Put([FromBody] BrandDto model)
        {
            if (!await _permissionService.Authorize(PermissionSystemName.Brands))
            {
                return(Forbid());
            }


            var brand = await _mediator.Send(new GetQuery <BrandDto>() { Id = model.Id });

            if (!brand.Any())
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                model = await _mediator.Send(new UpdateBrandCommand()
                {
                    Model = model
                });

                return(Ok(model));
            }

            return(BadRequest(ModelState));
        }
        public async Task <IActionResult> EditBrand(int id, BrandDto model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Something want wrong while updating brand"));
            }

            var brandToEdit = await _brandService.GetBrandByIdAsync(id);

            if (brandToEdit == null)
            {
                return(BadRequest("Brand not found"));
            }

            brandToEdit.Name = model.Name;

            await _brandService.UpdateBrandAsync(brandToEdit);

            await _genericRepository.SaveChangesAsync();

            return(Ok(new
            {
                status = 200,
                message = "Brand edited successfully"
            }));
        }
Example #18
0
        public async Task <ApiRequestResult> AddAsync(BrandDto dto)
        {
            var command = dto.EntityMap <BrandDto, Brand>();
            await _brandRepository.AddAsync(command);

            return(ApiRequestResult.Success("添加成功"));
        }
Example #19
0
        public void DtoToModelEntityTest()
        {
            var componentTypeDto = new ComponentTypeDto
            {
                Id   = 1,
                Name = "CPU"
            };

            var brandDto = new BrandDto
            {
                Id   = 1,
                Name = "Intel"
            };

            var componentDto = new ComponentDto
            {
                Id            = 1,
                Name          = "Intel 6600k",
                ComponentType = componentTypeDto,
                Brand         = brandDto,
                Price         = 100
            };

            var model = _componentMapper.ToEntityModel(componentDto);

            Assert.Equal(componentDto.Id, model.Id);
            Assert.Equal(componentDto.Name, model.Name);
            Assert.Equal(componentDto.Price, model.Price);
            Assert.Equal(componentDto.Brand.Id, model.Brand.Id);
            Assert.Equal(componentDto.Brand.Name, model.Brand.Name);
            Assert.Equal(componentDto.ComponentType.Id, model.ComponentType.Id);
            Assert.Equal(componentDto.ComponentType.Name, model.ComponentType.Name);
        }
Example #20
0
        public IEnumerable <BrandDto> GetAllBrands(string searchString)
        {
            List <BrandDto> result = new List <BrandDto>();
            var             query  = from b in MContext.Brands
                                     select new
            {
                BrandID   = b.ID,
                BrandName = b.Name
            };

            if (!String.IsNullOrEmpty(searchString))
            {
                query = query.Where(s => s.BrandName.Contains(searchString));
            }
            foreach (var item in query)
            {
                BrandDto brand = new BrandDto()
                {
                    ID   = item.BrandID,
                    Name = item.BrandName
                };
                result.Add(brand);
            }
            return(result.ToList());
        }
Example #21
0
 public static void MappingBrand(this BrandDto brandDto, Brand brand)
 {
     brand.Id      = brandDto.Id;
     brand.Name    = brandDto.Name;
     brand.Address = brandDto.Address;
     brand.Phone   = brandDto.Phone;
 }
Example #22
0
        private void gridViewBrand_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            try
            {
                var currentBrand = e.Row as BrandViewForUIDto;

                if (currentBrand != null)
                {
                    var toSave = new BrandDto {
                        Id = currentBrand.BrandId, Name = currentBrand.Brand, Models = new System.Collections.Generic.List <ModelDto>(), ProductType = new System.Collections.Generic.List <ProductTypeDto>()
                    };
                    var models = currentBrand.Models.Split(',');

                    foreach (var model in models)
                    {
                        toSave.Models.Add(new ModelDto {
                            Name = model
                        });
                    }

                    toSave.ProductType.Add(new ProductTypeDto {
                        Name = currentBrand.ProductType
                    });
                    var update = this.ServiceClient.SaveBrand(toSave);
                }

                this.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Properties.Resources.Error_Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Example #23
0
        //[ActionName("details")] api/brand/1
        public IHttpActionResult Put(int id, [FromBody] BrandDto dto, [UserId] int userId)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var item = _brandRepository.GetItem(id);

            if (item == null)
            {
                return(NotFound());
            }

            var createDate = item.CreatedDate;
            var createUser = item.CreatedUser;

            dto.Id           = id;
            item             = Mapper.Map <BrandDto, Brand>(dto, item);
            item.UpdatedDate = DateTime.Now;
            item.UpdatedUser = userId;
            item.CreatedDate = createDate;
            item.CreatedUser = createUser;

            item = CheckModel(item);


            ((IOPCRepository <int, Brand>)_brandRepository).Update(item);

            return(Get(id));
        }
Example #24
0
        public async Task <IActionResult> UpdateBrand([FromBody] BrandDto brand)
        {
            if (brand != null)
            {
                var _brand = await _repository.Brand.GetBrandById(brand.BrandId, brand.CompanyId);

                if (_brand != null)
                {
                    var __brand = _mapper.Map <CompanyBrandModel>(brand);
                    _brand.BrandName = __brand.BrandName;
                    _repository.Brand.UpdateBrand(_brand);
                    await _repository.Save();

                    var updatedBrand = _mapper.Map <BrandDto>(__brand);

                    var company = await _repository.Company.GetCompanyById(new Guid(updatedBrand.CompanyId));

                    if (company != null)
                    {
                        updatedBrand.CompanyName = company.CompanyName;
                    }

                    return(Ok(updatedBrand));
                }
                else
                {
                    return(StatusCode(500, "Something went wrong"));
                }
            }
            else
            {
                return(StatusCode(500, "Something went wrong"));
            }
        }
Example #25
0
        public async Task Delete(BrandDto dto)
        {
            var brand = await _brandRepository.GetById(dto.Id);

            if (brand != null)
            {
                var delete = await _brandRepository.GetById(brand.Id);

                if (delete.ErasedState)
                {
                    delete.ErasedState = false;

                    await _brandRepository.Update(delete);
                }
                else
                {
                    delete.ErasedState = true;

                    await _brandRepository.Update(delete);
                }
            }
            else
            {
                throw new Exception("This Brand not exist");
            }
        }
Example #26
0
        public bool Save(BrandDto brandDto)
        {
            Brand brand = new Brand
            {
                Id          = brandDto.Id,
                Name        = brandDto.Name,
                Country     = brandDto.Country,
                Description = brandDto.Description
            };

            try
            {
                using (UnitOfWork unitOfWork = new UnitOfWork())
                {
                    if (brandDto.Id == 0)
                    {
                        unitOfWork.BrandRepository.Insert(brand);
                    }
                    else
                    {
                        unitOfWork.BrandRepository.Update(brand);
                    }
                    unitOfWork.Save();
                }
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Example #27
0
        //Thêm Brand mới vào bảng MES_Audit_Brand
        public async Task <bool> Add(BrandDto model)
        {
            var brand = _mapper.Map <MES_Audit_Brand>(model);

            _repoBrand.Add(brand);
            return(await _repoBrand.SaveAll());
        }
Example #28
0
 public IActionResult AddOrEdit(int id, BrandDto brand)
 {
     if (ModelState.IsValid)
     {
         if (id == 0)
         {
             brandService.CreateBrand(brand);
         }
         else
         {
             try
             {
                 brandService.UpdateBrand(brand);
             }
             catch (DbUpdateConcurrencyException)
             {
                 if (!brandService.BrandExists(brand.Id))
                 {
                     return(NotFound());
                 }
                 else
                 {
                     throw;
                 }
             }
         }
         var brands = brandService.GetAll();
         return(Json(new { isValid = true, html = Helper.RenderRazorViewToString(this, "_ViewAll", brands) }));
     }
     return(Json(new { isValid = false, html = Helper.RenderRazorViewToString(this, "AddOrEdit", brand) }));
 }
Example #29
0
        public async Task <BrandDto> GetBrand(string title)
        {
            var      lowerTitle = title.ToLower();
            BrandDto brand      = await _brandRepo.FindBrand(lowerTitle);

            return(brand);
        }
Example #30
0
 public void Execute(BrandDto request)
 {
     request.Id = 0;
     _validator.ValidateAndThrow(request);
     _context.Brands.Add(_mapper.Map <Brand>(request));
     _context.SaveChanges();
 }
Example #31
0
 public ActionResult AddNewBrandName(BrandDto dto)
 {
     return this.Direct(new
     {
         success = !String.IsNullOrWhiteSpace(dto.Name),
         data = dto
     });
 }
Example #32
0
 public object Get(BrandDto request)
 {
     var brand = new BrandDto { Name = "hmm" };
     return brand;
 }