Esempio n. 1
0
        public IActionResult Post([FromBody] CustomerGroupForm model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var customergroup = new CustomerGroup
                    {
                        Name        = model.Name,
                        Description = model.Description,
                        IsActive    = model.IsActive
                    };

                    _customergroupRepository.Add(customergroup);
                    _customergroupRepository.SaveChanges();
                    return(Ok());
                }
            }
            catch (DbUpdateException ex)
            {
                if (ExceptionHelper.IsUniqueConstraintViolation(ex))
                {
                    ModelState.AddModelError("Name", $"The Name '{model.Name}' is already in use, please enter a different name.");
                    return(new BadRequestObjectResult(ModelState));
                }

                throw;
            }
            return(new BadRequestObjectResult(ModelState));
        }
Esempio n. 2
0
        public async Task <IActionResult> Put(long id, [FromBody] CustomerGroupForm model)
        {
            if (ModelState.IsValid)
            {
                var customergroup = await _customergroupRepository.Query().FirstOrDefaultAsync(x => x.Id == id);

                customergroup.Name        = model.Name;
                customergroup.Description = model.Description;
                customergroup.IsActive    = model.IsActive;
                customergroup.UpdatedOn   = DateTimeOffset.Now;

                try
                {
                    _customergroupRepository.SaveChanges();
                    return(Ok());
                }
                catch (DbUpdateException ex)
                {
                    if (ExceptionHelper.IsUniqueConstraintViolation(ex))
                    {
                        ModelState.AddModelError("Name", $"The Name '{model.Name}' is already in use, please enter a different name.");
                        return(new BadRequestObjectResult(ModelState));
                    }

                    throw;
                }
            }
            return(new BadRequestObjectResult(ModelState));
        }
Esempio n. 3
0
        public async Task <IActionResult> Get(long id)
        {
            var customergroup = await _customergroupRepository.Query().Include(x => x.Users).FirstOrDefaultAsync(x => x.Id == id);

            var model = new CustomerGroupForm
            {
                Id          = customergroup.Id,
                Name        = customergroup.Name,
                Description = customergroup.Description,
                IsActive    = customergroup.IsActive
            };

            return(Json(model));
        }
Esempio n. 4
0
        public async Task <IActionResult> Get(long id)
        {
            var customerGroup = await _customerGroupRepository.Query().FirstOrDefaultAsync(x => x.Id == id);

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

            var model = new CustomerGroupForm
            {
                Id          = customerGroup.Id,
                Name        = customerGroup.Name,
                Description = customerGroup.Description,
                IsActive    = customerGroup.IsActive
            };

            return(Json(model));
        }
Esempio n. 5
0
        public async Task <IActionResult> Post([FromBody] CustomerGroupForm model)
        {
            if (ModelState.IsValid)
            {
                var customerGroup = new CustomerGroup
                {
                    Name        = model.Name,
                    Description = model.Description,
                    IsActive    = model.IsActive
                };

                _customerGroupRepository.Add(customerGroup);
                await _customerGroupRepository.SaveChangesAsync();

                return(CreatedAtAction(nameof(Get), new { id = customerGroup.Id }, null));
            }

            return(BadRequest(ModelState));
        }
        public async Task <IActionResult> Put(long id, [FromBody] CustomerGroupForm model)
        {
            if (ModelState.IsValid)
            {
                var customerGroup = await _customerGroupRepository.Query().FirstOrDefaultAsync(x => x.Id == id);

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

                customerGroup.Name        = model.Name;
                customerGroup.Description = model.Description;
                customerGroup.IsActive    = model.IsActive;
                customerGroup.UpdatedOn   = DateTimeOffset.Now;

                await _customerGroupRepository.SaveChangesAsync();

                return(Accepted());
            }
            return(BadRequest(ModelState));
        }