Пример #1
0
        public IActionResult Search([FromBody] SearchParams searchParams)
        {
            Log(LogTypeEnum.Info, EventEnum.Search, null, $"Components search started");

            // Проверки на exception в Middleware
            using (var dbController = new DbController())
            {
                dbController.Connect();

                var searchComponents = dbController.SearchComponents(searchParams, out var errorMessages);

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

                    Log(LogTypeEnum.Warning, EventEnum.Search, false, $"Components search faild: {errorMessage}");

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

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

                dbController.SaveClose();

                Log(LogTypeEnum.Info, EventEnum.Search, true, $"Components search finished. Count {searchComponents.Components.Count}");

                return(new JsonResult(searchComponents));
            }
        }
Пример #2
0
        public IActionResult Update([FromBody] ComponentApiExist[] components)
        {
            var savedComponents = new List <ComponentApiExist>();

            Log(LogTypeEnum.Info, EventEnum.Update, null, $"Components update started");

            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 componentApiOut in components)
                {
                    var error       = string.Empty;
                    var dbComponent = componentConverter.ApiToDbExists(componentApiOut, out error);

                    if (dbComponent == null)
                    {
                        errorMessages.Add(error);

                        Log(LogTypeEnum.Warning, EventEnum.Update, false, $"Component update faild: {error}");
                    }
                    else
                    {
                        dbComponents.Add(dbComponent);

                        componentApiOut.ChangeDate = dbComponent.ChangeDate;
                        componentApiOut.CreateDate = dbComponent.CreateDate;

                        savedComponents.Add(componentApiOut);

                        Log(LogTypeEnum.Info, EventEnum.Update, true, $"Component update 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.SaveComponents(dbComponents);

                dbController.SaveClose();

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

            return(new JsonResult(savedComponents));
        }
Пример #3
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));
        }