Esempio n. 1
0
        public async Task <IActionResult> ReplaceSonar(string id, [FromBody] CreateSonarDto sonarDto)
        {
            if (sonarDto == null)
            {
                return(BadRequest("Put body could not be deserialized or was null"));
            }

            var sonar = _mapper.Map <Sonar>(sonarDto);

            sonar.Id       = Guid.ParseExact(id, "N");
            sonar.IsActive = true;

            try
            {
                _sonarsRepository.UpdateSonar(sonar);

                await _sonarsRepository.SaveChangesAsync();

                var result = _mapper.Map <SonarDto>(sonar);
                return(CreatedAtRoute("GetSonar", new { Controller = "Sonars", id }, result));
            }
            catch (SonarMissingException)
            {
                return(NotFound());
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> CreateSonar([FromBody] CreateSonarDto sonarDto)
        {
            if (sonarDto == null)
            {
                return(BadRequest("Post body could not be deserialized or was null"));
            }

            var sonar = _mapper.Map <Sonar>(sonarDto);

            sonar.IsActive = true;

            _sonarsRepository.CreateSonar(sonar);
            await _sonarsRepository.SaveChangesAsync();

            return(CreatedAtRoute("GetSonar", new { Controller = "Sonars", id = sonar.Id.ToString("N") },
                                  _mapper.Map <SonarDto>(sonar)));
        }
Esempio n. 3
0
        public async Task <IActionResult> UpdateSonar(string id, [FromBody] JsonPatchDocument <CreateSonarDto> sonarPatch)
        {
            if (sonarPatch == null)
            {
                return(BadRequest("Patch body could not be deserialized or was null"));
            }

            var sonarId = Guid.ParseExact(id, "N");
            var sonar   = _sonarsRepository.GetSonar(sonarId);

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

            var toPatch = new CreateSonarDto
            {
                Title     = sonar.Title,
                IpAddress = sonar.IpAddress,
                Mib       = sonar.Mib,
                IsActive  = true
            };

            sonarPatch.ApplyTo(toPatch);

            var patchedSonar = _mapper.Map <Sonar>(toPatch);

            try
            {
                _sonarsRepository.UpdateSonar(patchedSonar);
                await _sonarsRepository.SaveChangesAsync();

                return(CreatedAtRoute("GetSonar", new { Controller = "Sonars", id }, _mapper.Map <SonarDto>(sonar)));
            }
            catch (SonarMissingException)
            {
                return(NotFound());
            }
        }