示例#1
0
 public BaseResponse Post([FromBody]CategoryDto categoryDto)
 {
     Category category = Mapper.Map<Category>(categoryDto);
     databaseContext.Categories.Add(category);
     databaseContext.SaveChanges();
     return BaseResponse.CreateOkResponse(translationProvider);
 }
示例#2
0
 public BaseResponse Post([FromBody] IEnumerable <CategoryNodeDto> categoriesDto)
 {
     foreach (CategoryNodeDto categoryDto in categoriesDto)
     {
         SaveChangesInCategoryTree(categoryDto);
     }
     databaseContext.SaveChanges();
     return(BaseResponse.CreateOkResponse(translationProvider));
 }
示例#3
0
        public BaseResponse Put(int id, [FromBody]string name)
        {
            Category category = new Category();
            category.Name = name;

            databaseContext.Categories.Add(category);
            databaseContext.SaveChanges();
            return BaseResponse.CreateOkResponse(translationProvider);
        }
示例#4
0
        public async Task <IHttpActionResult> Register(RegisterRequest model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            IdentityResult result;

            using (var context = new ApplicationDbContext())
            {
                var roleStore   = new RoleStore <IdentityRole>(context);
                var roleManager = new RoleManager <IdentityRole>(roleStore);

                await roleManager.CreateAsync(new IdentityRole()
                {
                    Name = "User"
                });

                await roleManager.CreateAsync(new IdentityRole()
                {
                    Name = "Administrator"
                });

                var userStore   = new UserStore <ApplicationUser>(context);
                var userManager = new UserManager <ApplicationUser>(userStore);

                var user = new ApplicationUser()
                {
                    UserName = model.Email, Email = model.Email
                };

                result = await UserManager.CreateAsync(user, model.Password);

                if (!result.Succeeded)
                {
                    string message = String.Join(";", result.Errors);
                    return(Content(HttpStatusCode.OK, new BaseResponse()
                    {
                        Message = message,
                        Success = false
                    }));
                }
                await userManager.AddToRoleAsync(user.Id, "User");

                await userManager.AddToRoleAsync(user.Id, "Administrator");
            }

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            return(Content(HttpStatusCode.OK, BaseResponse.CreateOkResponse(translationProvider)));
        }
示例#5
0
 public BaseResponse Delete(int id)
 {
     var category = databaseContext.Categories.FirstOrDefault(c => c.Id == id);
     if(category == null)
     {
         return BaseResponse.CreateNotFoundResponse(id, translationProvider);
     }
     databaseContext.Categories.Remove(category);
     databaseContext.SaveChanges();
     return BaseResponse.CreateOkResponse(translationProvider);
 }
示例#6
0
        public BaseResponse Delete(long id)
        {
            var product = databaseContext.Products.FirstOrDefault(c => c.Id == id);

            if (product == null)
            {
                return(BaseResponse.CreateNotFoundResponse(id, translationProvider));
            }
            databaseContext.Products.Remove(product);
            databaseContext.SaveChanges();
            return(BaseResponse.CreateOkResponse(translationProvider));
        }
示例#7
0
        public BaseResponse Post([FromBody] ProductDto productDto)
        {
            var validateResponse = ValidateProductDto(productDto);

            if (!validateResponse.Success)
            {
                return(validateResponse);
            }

            Product product = Mapper.Map <Product>(productDto);

            databaseContext.Products.Add(product);
            databaseContext.SaveChanges();
            return(BaseResponse.CreateOkResponse(translationProvider));
        }
示例#8
0
        private BaseResponse SaveChangesInCategoryTree(CategoryNodeDto categoryDto)
        {
            Category category       = Mapper.Map <Category>(categoryDto);
            Category categoryEntity = databaseContext.Categories.Where(cat => cat.Id == category.Id).FirstOrDefault();

            if (categoryEntity == null)
            {
                return(BaseResponse.CreateNotFoundResponse(categoryDto.Id, translationProvider));
            }
            foreach (CategoryNodeDto child in categoryDto.Children)
            {
                SaveChangesInCategoryTree(child);
            }
            databaseContext.Entry(categoryEntity).State = EntityState.Modified;
            databaseContext.Entry(categoryEntity).CurrentValues.SetValues(categoryDto);
            return(BaseResponse.CreateOkResponse(translationProvider));
        }
示例#9
0
        public BaseResponse Put([FromBody] ProductDto productDto)
        {
            Product productEntity = databaseContext.Products.Where(prod => prod.Id == productDto.Id).FirstOrDefault();

            if (productEntity == null)
            {
                return(BaseResponse.CreateNotFoundResponse(productDto.Id, translationProvider));
            }
            var validateResponse = ValidateProductDto(productDto);

            if (!validateResponse.Success)
            {
                return(validateResponse);
            }

            databaseContext.Entry(productEntity).State = EntityState.Modified;
            databaseContext.Entry(productEntity).CurrentValues.SetValues(productDto);
            databaseContext.SaveChanges();
            return(BaseResponse.CreateOkResponse(translationProvider));
        }