Ejemplo n.º 1
0
        public IActionResult CreateCategory([FromBody] CategoryCreateDto categoryDto)
        {
            if (categoryDto == null)
            {
                return(BadRequest(new { message = ModelStateToString.ConvertModelStateToString(ModelState) }));
            }

            if (_repo.CategoryExists(categoryDto.Title))
            {
                return(StatusCode(400, new { message = "Category with this title already exists" }));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(new { message = ModelStateToString.ConvertModelStateToString(ModelState) }));
            }

            var categoryObj = _mapper.Map <Category>(categoryDto);

            categoryObj.CreationDateTime = DateTime.Now;
            if (!_repo.CreateCategory(categoryObj))
            {
                return(StatusCode(500, new { message = $"Something went wrong when saving the record {categoryDto.Title}" }));
            }

            return(CreatedAtRoute("GetCategory", new { Version = HttpContext.GetRequestedApiVersion().ToString(), id = categoryObj.Id }, categoryObj));
        }
Ejemplo n.º 2
0
        public ActionResult <CategoryReadDTO> CreateCategory([FromForm] CategoryCreateDTO categoryCreateDTO)
        {
            // SUBE UNA SOLA IMAGEN A UN CAMPO DE TIPO Byte[] en una tabla
            if (categoryCreateDTO.Files.FileName != null)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    categoryCreateDTO.Files.OpenReadStream().CopyTo(ms);
                    // return arreglo de bytes
                    categoryCreateDTO.Picture = ms.ToArray();
                }
            }

            var categoryModel = _mapper.Map <Categories>(categoryCreateDTO);

            _repo.CreateCategory(categoryModel);
            _repo.SaveChanges();

            var categoryReadDTO = _mapper.Map <CategoryReadDTO>(categoryModel);

            // Decodificar base64 online
            // https://base64.guru/converter/decode/image
            // https://codebeautify.org/base64-to-image-converter

            return(CreatedAtRoute(nameof(GetCategoryById), new { id = categoryReadDTO.CategoryId }, categoryReadDTO));
            //return Ok(categoryReadDTO);
        }
        public async Task CreateCategory([FromBody] CategoryCreateDto CategoryCreateDto)
        {
            var categoryModel = _mapper.Map <Category>(CategoryCreateDto);
            await _repo.CreateCategory(categoryModel);

            _repo.SaveChanges();
        }
Ejemplo n.º 4
0
        public ActionResult <CategoryReadDto> CreateCategory(CategoryCreateDto categoryCreateDto)
        {
            var categoryModel = _mapper.Map <Category>(categoryCreateDto);

            _repository.CreateCategory(categoryModel);
            _repository.SaveChanges();
            var categoryReadDto = _mapper.Map <CategoryReadDto>(categoryModel);

            return(CreatedAtRoute(nameof(GetCategoryById), new { id = categoryReadDto.id }, categoryReadDto));
        }
Ejemplo n.º 5
0
 public ActionResult Create(Category category)
 {
     if (ModelState.IsValid)
     {
         ResponseMessage = _categoryRepo.CreateCategory(category);
         return(RedirectToAction("Index"));
     }
     categoryViewModel.Category  = category;
     categoryViewModel.PageTitle = "Create Category";
     return(View(categoryViewModel));
 }
Ejemplo n.º 6
0
 public void CreateCategory(string title, string parentCategory = null)
 {
     if (parentCategory != null && !CategoryExists(parentCategory))
     {
         throw new Exception($"Category with title: {parentCategory} does not exists");
     }
     if (CategoryExists(title))
     {
         throw new Exception($"Category with title: {title} already exists");
     }
     CategoryRepo.CreateCategory(new Category(title, parentCategory));
 }
Ejemplo n.º 7
0
        public IActionResult Create([Bind("Id,Name")] Category category)
        {
            if (ModelState.IsValid)
            {
                _categoryRepo.CreateCategory(category);

                _categoryRepo.SaveChanges();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
Ejemplo n.º 8
0
        public int Save()
        {
            var create = GetAggregaterootId() <= 0;
            var siteId = _siteRepository.SaveSite(this);

            value.SiteId = siteId;
            if (create)
            {
                var cat = new CmsCategoryEntity
                {
                    Code        = "",
                    Tag         = "default",
                    ParentId    = 0,
                    SiteId      = GetAggregaterootId(),
                    Flag        = (int)CategoryFlag.Enabled,
                    Name        = "默认栏目",
                    Path        = "default",
                    Description = "",
                    Icon        = "",
                    Keywords    = "",
                    Location    = "",
                    SortNumber  = 0,
                    Title       = ""
                };
                var ic  = _categoryRep.CreateCategory(cat);
                var err = ic.Set(cat);
                if (err == null)
                {
                    err = ic.Save();
                }
                if (err != null)
                {
                    throw new Exception("初始化站点目录失败:" + err.Message);
                }
            }

            return(GetAggregaterootId());
        }
Ejemplo n.º 9
0
        public async Task <IActionResult> Create([FromRoute] ulong server, [FromForm] string name)
        {
            var authEntry = (AuthEntry)HttpContext.Items["key"];

            if (authEntry is null)
            {
                return(Redirect("/login"));
            }
            var userGuilds = await userService.GetAllowedUserGuilds(authEntry);

            if (!userGuilds.Any(x => x.Id == server))
            {
                return(Unauthorized());
            }

            if (!ValidateName(name, out var cleanedName))
            {
                return(BadRequest("Invalid category name"));
            }

            await categoryRepo.CreateCategory(cleanedName, server);

            return(RedirectToAction("Index", new { server = server }));
        }
Ejemplo n.º 10
0
        public Result SaveCategory(int siteId, int parentId, CategoryDto category)
        {
            ISite     site = this.repo.GetSiteById(siteId);
            ICategory ic   = null;

            if (category.ID > 0)
            {
                ic = site.GetCategory(category.ID);
            }
            if (ic == null)
            {
                ic = _categoryRep.CreateCategory(new CmsCategoryEntity());
            }
            CmsCategoryEntity cat = new CmsCategoryEntity();

            cat.SiteId      = siteId;
            cat.Keywords    = category.Keywords;
            cat.Description = category.Description;
            cat.ParentId    = parentId;
            cat.Tag         = category.Tag;
            cat.Icon        = category.Icon;
            cat.Name        = category.Name;
            cat.Title       = category.PageTitle;
            cat.SortNumber  = category.SortNumber;
            cat.ModuleId    = category.ModuleId;
            cat.Location    = category.Location;
            Error err = ic.Set(cat);

            if (err == null)
            {
                // 保存模板
                List <TemplateBind> binds = new List <TemplateBind>();
                if (!String.IsNullOrEmpty(category.CategoryTemplate))
                {
                    binds.Add(new TemplateBind(0, TemplateBindType.CategoryTemplate, category.CategoryTemplate));
                }
                if (!String.IsNullOrEmpty(category.CategoryArchiveTemplate))
                {
                    binds.Add(new TemplateBind(0, TemplateBindType.CategoryArchiveTemplate, category.CategoryArchiveTemplate));
                }
                err = ic.SetTemplates(binds.ToArray());
                if (err == null)
                {
                    // 扩展属性
                    if (category.ExtendFields != null)
                    {
                        ic.ExtendFields = category.ExtendFields;
                    }
                    if (err == null)
                    {
                        err = ic.Save();
                    }
                }
            }

            #region 扩展属性



            #endregion

            Result r = new Result();
            if (err == null)
            {
                r.Data = new Dictionary <String, String>();
                r.Data["CategoryId"] = ic.GetDomainId().ToString();
            }
            else
            {
                r.ErrCode = 1;
                r.ErrMsg  = err.Message;
            }
            return(r);
        }
Ejemplo n.º 11
0
        public async Task <Response <Category> > AddCategory(Category category)
        {
            var response = await categoryRepo.CreateCategory(category);

            return(Response <Category> .Ok(response));
        }
Ejemplo n.º 12
0
 public ActionResult <Category> CreateCategory(Category createCategory)
 {
     _repository.CreateCategory(createCategory);
     _repository.SaveChanges();
     return(createCategory);
 }