Exemplo n.º 1
0
        public async Task <IActionResult> Update(int id, [FromBody] SaveConfigurationResource configurationResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var StartTime1 = configurationResource.LockedBusOrderStart1;
            var EndTime1   = configurationResource.LockedBusOrderEnd1;
            var StartTime2 = configurationResource.LockedBusOrderStart2;
            var EndTime2   = configurationResource.LockedBusOrderEnd2;

            if (
                TimeSpan.Parse(EndTime1) < TimeSpan.Parse(StartTime1) ||
                TimeSpan.Parse(EndTime2) < TimeSpan.Parse(StartTime2) ||
                TimeSpan.Parse(StartTime2) < TimeSpan.Parse(StartTime1)
                )
            {
                return(BadRequest("Time Setting Invalid"));
            }

            var configuration = await configurationRepository.GetOne(id);

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

            configuration = mapper.Map(configurationResource, configuration);

            configurationRepository.Update(configuration);

            if (await unitOfWork.CompleteAsync() == false)
            {
                throw new Exception(message: $"Updating configuration with id: {id} failed on save");
            }

            configuration = await configurationRepository.GetOne(configuration.Id);

            var result = mapper.Map <AppConfiguration, ViewConfigurationResource>(configuration);

            return(Ok(result));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create([FromBody] SaveConfigurationResource configurationResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var configuration = mapper.Map <SaveConfigurationResource, AppConfiguration>(configurationResource);

            configurationRepository.Add(configuration);

            if (await unitOfWork.CompleteAsync() == false)
            {
                throw new Exception(message: "Create new configuration fail on save");
            }

            configuration = await configurationRepository.GetOne(configuration.Id);

            var result = mapper.Map <AppConfiguration, ViewConfigurationResource>(configuration);

            return(Ok(result));
        }