예제 #1
0
        public ActionResult Post(ArgumentClass arguments)
        {
            if (!_managerLoginAdmin.IsStatusOK(HttpContext))
            {
                return(Forbid());
            }

            if (string.IsNullOrWhiteSpace(arguments.urlImages))
            {
                ModelState.AddModelError("urls", "пустое поле");
            }

            if (string.IsNullOrWhiteSpace(arguments.Name))
            {
                ModelState.AddModelError("name", "пустое поле");
            }

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

            Work work = new Work()
            {
                Name        = arguments.Name,
                Description = arguments.Description,
                Address     = arguments.Address,
                Prestige    = arguments.Prestige
            };

            List <Image> images = new();

            foreach (string url in arguments.urlImages.Split('\n'))
            {
                images.Add(new Image()
                {
                    url = url
                });
            }
            work.Images.AddRange(images);

            db.Images.AddRange(images);
            db.Works.Add(work);

            try
            {
                db.SaveChanges();
                _logger.LogAddWorkToDB(work);

                return(Ok());
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);

                return(BadRequest(ex));
            }
        }