public async Task <IActionResult> EditBrand(int id, BrandRequest model)
        {
            DataResponse response = new DataResponse();

            try
            {
                if (!ModelState.IsValid)
                {
                    response.Message = string.Join("", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage));
                    return(BadRequest(response));
                }

                var brand = await _brandService.GetById(id);

                if (brand == null)
                {
                    response.Message = "The brand is not found.";
                    return(NotFound(response));
                }
                await _brandService.Edit(brand, model);

                response.Success = 1;
                response.Message = "The brand has been updated.";
                return(Ok(response));
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return(StatusCode(500, response));
            }
        }
        public async Task <GlobalResponse> Add(Guid businessId, BrandRequest request)
        {
            var business = await DbContext.Businesses.Include(x => x.Brands).FirstOrDefaultAsync(x => x.Id == businessId);

            if (business is null)
            {
                throw new KeyNotFoundException("Business not found");
            }
            var check = business.Brands.FirstOrDefault(x => string.Equals(x.Name, request.Name, StringComparison.CurrentCultureIgnoreCase));

            if (!(check is null))
            {
                throw new AppException("Brand already exist");
            }
            var brand = new Brand()
            {
                Name = request.Name, Description = request.Description
            };

            business.Brands.Add(brand);
            DbContext.Update(business);
            await DbContext.SaveChangesAsync();

            return(new GlobalResponse()
            {
                Message = "Successful", Status = true
            });
        }
        public async Task Add(BrandRequest model)
        {
            Brand brand = new Brand {
                Name = model.Name, Discontinued = false
            };

            _context.Brands.Add(brand);
            await _context.SaveChangesAsync();
        }
        public IActionResult Modify([FromBody] BrandRequest brandRequest)
        {
            Brand brand = AutoMap.Map <BrandRequest, Brand>(brandRequest);

            this._brandService.Modify(brand.Identity, brand);

            BrandResponse brandResponses = AutoMap.Map <BrandRequest, BrandResponse>(brandRequest);

            return(Ok(new BaseResponse <BrandResponse>(brandResponses)));
        }
        public IActionResult Add([FromBody] BrandRequest brandRequest)
        {
            Brand brand = AutoMap.Map <BrandRequest, Brand>(brandRequest);

            brand.Identity = default;

            BrandResponse brandResponses = AutoMap.Map <Brand, BrandResponse>(this._brandService.Add(brand));

            return(Ok(new BaseResponse <BrandResponse>(brandResponses)));
        }
        public async Task <IActionResult> AddBrand(BrandRequest model)
        {
            DataResponse response = new DataResponse();

            try
            {
                if (!ModelState.IsValid)
                {
                    response.Message = string.Join("", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage));
                    return(BadRequest(response));
                }
                await _brandService.Add(model);

                response.Success = 1;
                response.Message = "The brand has been added.";
                return(Ok(response));
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return(StatusCode(500, response));
            }
        }
        public async Task <string> AddBrand(BrandRequest request)
        {
            try
            {
                using var sqlCon = new SqlConnection(_options.connectionString);
                await sqlCon.OpenAsync();

                var dynamicParameters = new DynamicParameters();
                dynamicParameters.Add("@BrandName", request.Name);
                var res = new BrandResponse();
                await sqlCon.ExecuteAsync(
                    "spAddBrand",
                    dynamicParameters,
                    commandType : CommandType.StoredProcedure);

                return(res.Messages = Enumhelper.GetEnumDescription(StatusHandlersEnum.StatusHandle.CreateSuccess));
            }
            catch (Exception ex)
            {
                var res = new BrandResponse();
                return(res.Messages = ex.Message);
            }
        }
 public async Task Edit(Brand brand, BrandRequest model)
 {
     brand.Name = model.Name;
     _context.Entry(brand).State = EntityState.Modified;
     await _context.SaveChangesAsync();
 }
Beispiel #9
0
 public async Task <string> UpdateBrand([FromBody] BrandRequest request)
 {
     return(await _brandService.UpdateBrand(request));
 }
        public async Task <IActionResult> Create([FromRoute] Guid businessId, [FromBody] BrandRequest request)
        {
            var res = await BrandService.Add(businessId, request);

            return(Ok(res));
        }