Ejemplo n.º 1
0
        public IActionResult Create([FromBody] ComponentApiNew[] components)
        {
            Log(LogTypeEnum.Info, EventEnum.Create, null, "Components creation started");
            //var r = User.Claims;
            var savedComponents = new List <ComponentApiExist>();

            using (var dbController = new DbController())
            {
                dbController.Connect();

                var componentConverter = new ComponentConverter(dbController, _settingsService);

                var dbComponents = new List <Component>();

                var errorMessages = new List <string>();

                foreach (var componentObject in components)
                {
                    var error       = string.Empty;
                    var dbComponent = componentConverter.ApiInToDbNew(componentObject, out error);

                    if (dbComponent == null)
                    {
                        Log(LogTypeEnum.Warning, EventEnum.Create, false, $"Component creation faild: {error}");

                        errorMessages.Add(error);
                    }
                    else
                    {
                        dbComponents.Add(dbComponent);

                        var savedComponent = new ComponentApiExist
                        {
                            Id         = dbComponent.Id,
                            CreateDate = dbComponent.CreateDate,
                            ChangeDate = dbComponent.CreateDate,
                            TypeName   = componentObject.TypeName,
                            Attributes = componentObject.Attributes
                        };

                        savedComponents.Add(savedComponent);

                        Log(LogTypeEnum.Info, EventEnum.Create, true, $"Component creation with id '{dbComponent.Id}' is ready");
                    }
                }

                if (errorMessages.Count != 0)
                {
                    var errorMessage = string.Join(';', errorMessages);

                    return(new BadRequestObjectResult(new ErrorDetails {
                        StatusCode = (int)HttpStatusCode.BadRequest, Message = errorMessage
                    }));
                }

                dbController.AddComponents(dbComponents);

                dbController.SaveClose();

                Log(LogTypeEnum.Info, EventEnum.Create, true, $"Components creation finished. Count {savedComponents.Count}");
            }

            return(new JsonResult(savedComponents));
        }