예제 #1
0
        public IActionResult CreateJob([FromBody] Job job)
        {
            try
            {
                if (job.IsObjectNull())
                {
                    _logger.LogError($"Job object sent from client is null.");
                    return(BadRequest($"Job object is null."));
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogError($"Job object sent from client is invalid.");
                    return(BadRequest($"Job object is invalid."));
                }

                var device = _repoWrapper.Devices.GetDeviceById(job.DeviceId);
                if (device.IsObjectNull())
                {
                    _logger.LogError($"Device with ID: {job.DeviceId} couldn't be found.");
                    return(NotFound("Device with such ID couldn't be found."));
                }

                job.Id = _scheduleManager.AddJob(device, job.Start, job.End, job.DaysList.ToArray());
                _repoWrapper.Job.CreateJob(job);
                return(CreatedAtRoute("JobById", new { id = job.Id }, job));
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside CreateJob action: {e.Message}");
                return(StatusCode(500, "Internal Server Error"));
            }
        }