Пример #1
0
        public async Task <IActionResult> PostTaskAsync([FromBody] PostTaskRequest request)
        {
            Logger?.LogDebug("'{0}' has been invoked", nameof(PostTaskAsync));

            var response = new SingleResponse <Task>();

            try
            {
                var existingEntity = await DbContext.GetTaskByNameAsync(new Task { Name = request.Name });

                if (existingEntity != null)
                {
                    ModelState.AddModelError("TaskName", "Task name already exists");
                }

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

                var entity = request.ToEntity();

                DbContext.Add(entity);

                await DbContext.SaveChangesAsync();

                response.Model = entity;
            }
            catch (Exception ex)
            {
                response.DidError     = true;
                response.ErrorMessage = "There was an internal error, please contact to technical support";

                Logger?.LogCritical("There was an error on '{0}' invocation: {1}", nameof(PostTaskAsync), ex);
            }
            return(response.ToHttpResponse());
        }