Ejemplo n.º 1
0
        public async Task <ActionResult> Add(BrandDtos brandDtos)
        {
            try
            {
                var exit = await _brandRepository.GetallAsyn();

                foreach (var see in exit)
                {
                    if (see.Name == brandDtos.Name)
                    {
                        return(BadRequest("This brand already exists, Please enter a different one to this one."));
                    }
                }

                var brand = _mapper.Map <Brand>(brandDtos);

                var NewBrand = await _brandRepository.Add(brand);

                if (brand == null)
                {
                    return(BadRequest("Could not create a new Brand."));
                }

                return(Ok("A new brand has been created."));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Update(int id, BrandDtos dtos)
        {
            try
            {
                var exist = await this._brandRepository.GetOneAsyn(id);

                if (exist == null)
                {
                    return(NotFound($"No Brand found with this id {id}."));
                }


                if (exist.Name != dtos.Name)
                {
                    var sameName = await _brandRepository.GetallAsyn();

                    foreach (var see in sameName)
                    {
                        if (see.Name == dtos.Name)
                        {
                            return(BadRequest("This brand already exists, Please enter a different one to this one."));
                        }
                    }
                }


                var brand = _mapper.Map <Brand>(dtos);

                var data = await _brandRepository.Update(id, brand);

                if (!data)
                {
                    return(BadRequest("This brand could not be updated."));
                }

                return(Ok("Changes made."));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }