예제 #1
0
        public async Task <IActionResult> UpdateDeviceAttributeType(int id, [FromBody] DeviceAttributeTypeSaveResource deviceAttributeTypeSaveResource)
        {
            if (!_auth.IsAppAdmin(User))
            {
                return(NoContent());
            }

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

            var deviceAttributeTypeFromRepo = await _repo.GetDeviceAttributeType(id);

            if (deviceAttributeTypeFromRepo == null)
            {
                return(BadRequest($"DeviceAttributeTypeId {id} could not be found"));
            }

            /* Test for prexistence */
            var filter = new MdaDeviceAttributeTypeQuery()
            {
                Name   = deviceAttributeTypeSaveResource.Name,
                Active = 2
            };

            var deviceAttributeTypeFromRepoExisting = await _repo.GetDeviceAttributeTypes(filter);

            if (deviceAttributeTypeFromRepoExisting.Any())
            {
                var existingElement = deviceAttributeTypeFromRepoExisting.FirstOrDefault();
                if (existingElement.Id != id)
                {
                    return(BadRequest($"AttributeType {deviceAttributeTypeSaveResource.Name} already exists"));
                }
                else
                {
                    if (existingElement.Name.ToLower() == deviceAttributeTypeSaveResource.Name.ToLower())
                    {
                        if (existingElement.Active == Convert.ToByte(deviceAttributeTypeSaveResource.Active == true ? 1 : 0))
                        {
                            return(BadRequest("Nothing has changed"));
                        }
                    }
                }
            }

            _mapper.Map <DeviceAttributeTypeSaveResource, MdaDeviceAttributeType>(deviceAttributeTypeSaveResource, deviceAttributeTypeFromRepo);
            deviceAttributeTypeFromRepo.ModifiedBy   = User.Identity.Name;
            deviceAttributeTypeFromRepo.ModifiedDate = DateTime.Now;

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            return(BadRequest("Failed to update device date type"));
        }
예제 #2
0
        public async Task <IActionResult> AddDeviceAttributeType([FromBody] DeviceAttributeTypeSaveResource deviceAttributeTypeSaveResource)
        {
            if (!_auth.IsAppAdmin(User))
            {
                return(NoContent());
            }

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

            /* Test for prexistence */
            var deviceAttributeTypeFromRepo = await _repo.GetDeviceAttributeTypes(new MdaDeviceAttributeTypeQuery()
            {
                Name = deviceAttributeTypeSaveResource.Name
            });

            if (deviceAttributeTypeFromRepo.Any())
            {
                return(BadRequest($"AttributeType {deviceAttributeTypeSaveResource.Name} already exists"));
            }

            var deviceAttributeType = _mapper.Map <MdaDeviceAttributeType>(deviceAttributeTypeSaveResource);

            deviceAttributeType.CreatedBy = User.Identity.Name;

            _repo.Add(deviceAttributeType);

            if (await _repo.SaveAll())
            {
                return(Ok(deviceAttributeType));
            }

            return(BadRequest("Failed to add device date"));
        }