Beispiel #1
0
        public async Task <IActionResult> PostCategory([FromBody] CategoryCreateRequest request)
        {
            //// CREATE A CONSTANCE OF CATEGORY WITH INFORS ARE INPUT DATA
            var category = new Category()
            {
                Name           = request.Name,
                ParentId       = request.ParentId,
                SortOrder      = request.SortOrder,
                SeoAlias       = request.SeoAlias,
                SeoDescription = request.SeoDescription
            };

            //// INSERT NEW CATEGORY INTO DATABASE AND SAVE CHANGE
            _context.Categories.Add(category);
            var result = await _context.SaveChangesAsync();

            //// IF RESULT AFTER INSERT IS GREATER THAN 0 (TRUE), RETURN STATUS 201, ELSE RETURN STATUS 400
            if (result > 0)
            {
                await _cacheService.RemoveAsync(CacheConstants.Categories);

                return(CreatedAtAction(nameof(GetById), new { id = category.Id }, request));
            }
            else
            {
                return(BadRequest(new ApiBadRequestResponse("Create category failed")));
            }
        }
        public async Task <IActionResult> PostFunction([FromBody] FunctionCreateRequest request)
        {
            //// GET FUNCTION WITH ID (KEY)
            var dbFunction = await _context.Functions.FindAsync(request.Id);

            //// IF RESULT NOT NULL, FUNCTION ALREADY EXISTS, RETURN STATUS 400
            if (dbFunction != null)
            {
                return(BadRequest(new ApiBadRequestResponse($"Function with id {request.Id} is existed.")));
            }
            //// CREATE A INSTANCE OF FUNCTION WITH INFO IS INPUT DATA
            var function = new Function()
            {
                Id        = request.Id,
                Name      = request.Name,
                ParentId  = request.ParentId,
                SortOrder = request.SortOrder,
                Url       = request.Url
            };

            //// INSERT NEW FUNCTION INTO DATATABLE IN DATABASE AND SAVE CHANGE
            _context.Functions.Add(function);
            var result = await _context.SaveChangesAsync();

            //// IF RESULT AFTER ADD GREATER 0, INSERT SUCCESS AND RETURN STATUS 201, ELSE RETURN STATUS IS 400
            if (result > 0)
            {
                return(CreatedAtAction(nameof(GetById), new { id = function.Id }, request));
            }
            else
            {
                return(BadRequest(new ApiBadRequestResponse("Create function is failed")));
            }
        }
        public async Task <IActionResult> PostKnowledgeBase([FromForm] KnowledgeBaseCreateRequest request)
        {
            //// CREATE NEW INSTANCE OF KNOWLEDGE BASE WITH INFOS ARE INPUT DATA
            KnowledgeBase knowledgeBase = CreateKnowledgeBaseEntity(request);

            knowledgeBase.Id = await _sequenceService.GetKnowledgeBaseNewId();

            //// GET CURRENT ID (IS OWNER USER)
            knowledgeBase.OwnerUserId = User.GetUserId();
            //// CONVERT SIGN STRING TO UNSIGN STRING
            if (string.IsNullOrEmpty(knowledgeBase.SeoAlias))
            {
                knowledgeBase.SeoAlias = TextHelper.ToUnsignString(knowledgeBase.Title);
            }

            //// PROCESS ATTACHMENT
            if (request.Attachments != null && request.Attachments.Count > 0)
            {
                foreach (var attachment in request.Attachments)
                {
                    var attachmentEntity = await SaveFile(knowledgeBase.Id, attachment);

                    _context.Attachments.Add(attachmentEntity);
                }
            }

            _context.KnowledgeBases.Add(knowledgeBase);

            //// PROCESS LABEL
            if (request.Labels?.Length > 0)
            {
                await ProcessLabel(request, knowledgeBase);
            }

            //// INSERT NEW KNOWLEDGE BASE INTO DATABASE AND SAVE CHANGE
            _context.KnowledgeBases.Add(knowledgeBase);
            var result = await _context.SaveChangesAsync();

            //// IF RESULT AFTER INSERT IS GREATER THAN 0 (TRUE), RETURN STATUS 201, ELSE RETURN STATUS 400
            if (result > 0)
            {
                await _cacheService.RemoveAsync(CacheConstants.LatestKnowledgeBases);

                await _cacheService.RemoveAsync(CacheConstants.PopularKnowledgeBases);

                return(CreatedAtAction(nameof(GetById), new { id = knowledgeBase.Id }));
            }
            else
            {
                return(BadRequest(new ApiBadRequestResponse("Create knowledge base failed")));
            }
        }
        public async Task <IActionResult> PutPermissionByRoleId(string roleId, [FromBody] UpdatePermissionRequest request)
        {
            //create new permission list from user changed
            //// CREATE NEW LIST PERMISSION TO SAVE PERMISSION THOSE USER CHANGED
            var newPermissions = new List <Permission>();

            foreach (var p in request.Permissions)
            {
                newPermissions.Add(new Permission(p.FunctionId, roleId, p.CommandId));
            }

            //// GET ALL PERMISSION WITH ROLE ID (KEY OF ROLE) WHICH EXIST IN DATABASE AND REMOVE THEM
            var existingPermissions = _context.Permissions.Where(x => x.RoleId == roleId);

            _context.Permissions.RemoveRange(existingPermissions);

            //// ADD NEW PERMISSIONS INTO DATABASE TO CHANGE PERMISSION OF THAT ROLE AND SAVE CHANGE
            _context.Permissions.AddRange(newPermissions.Distinct(new MyPermissionComparer()));
            var result = await _context.SaveChangesAsync();

            //// IF RESULT AFTER UPDATE IS GREATER THAN 0 (TRUE), RETURN STATUS 201, ELSE RETURN 400
            if (result > 0)
            {
                return(NoContent());
            }
            return(BadRequest(new ApiBadRequestResponse("Save permission failed")));
        }
        public async Task GetCommands_HasData_ReturnSuccess()
        {
            _context.Commands.AddRange(new List <Command>()
            {
                new Command()
                {
                    Id = "TEST1", Name = "Xem"
                },
                new Command()
                {
                    Id = "TEST2", Name = "Thêm"
                },
                new Command()
                {
                    Id = "TEST3", Name = "Sửa"
                },
                new Command()
                {
                    Id = "TEST4", Name = "Xoá"
                },
                new Command()
                {
                    Id = "TEST5", Name = "Duyệt"
                },
            });
            await _context.SaveChangesAsync();

            var commandsController = new CommandsController(_context);
            var result             = await commandsController.GetCommands();

            var okResult    = result as OkObjectResult;
            var FunctionVms = okResult.Value as IEnumerable <CommandVm>;

            Assert.True(FunctionVms.Count() > 0);
        }
        public async Task GetFunction_HasData_ReturnSuccess()
        {
            _context.Functions.AddRange(new List <Function>()
            {
                new Function()
                {
                    Id        = "GetFunction_HasData_ReturnSuccess",
                    ParentId  = null,
                    Name      = "GetFunction_HasData_ReturnSuccess",
                    SortOrder = 1,
                    Url       = "/GetFunction_HasData_ReturnSuccess"
                }
            });
            await _context.SaveChangesAsync();

            var functionsController = new FunctionsController(_context);
            var result = await functionsController.GetFunctions();

            var okResult    = result as OkObjectResult;
            var FunctionVms = okResult.Value as IEnumerable <FunctionVm>;

            Assert.True(FunctionVms.Count() > 0);
        }
        public CommandInFunctionControllerTest()
        {
            _context = new InMemoryDbContextFactory().GetApplicationDbContext();

            _context.Functions.AddRange(new List <Function>()
            {
                new Function()
                {
                    Id        = "GetFunctionsPaging_NoFilter_ReturnSuccess1",
                    ParentId  = null,
                    Name      = "GetFunctionsPaging_NoFilter_ReturnSuccess1",
                    SortOrder = 1,
                    Url       = "/test1"
                },
                new Function()
                {
                    Id        = "GetFunctionsPaging_NoFilter_ReturnSuccess2",
                    ParentId  = null,
                    Name      = "GetFunctionsPaging_NoFilter_ReturnSuccess2",
                    SortOrder = 2,
                    Url       = "/test2"
                },
                new Function()
                {
                    Id        = "GetFunctionsPaging_NoFilter_ReturnSuccess3",
                    ParentId  = null,
                    Name      = "GetFunctionsPaging_NoFilter_ReturnSuccess3",
                    SortOrder = 3,
                    Url       = "/test3"
                },
                new Function()
                {
                    Id        = "GetFunctionsPaging_NoFilter_ReturnSuccess4",
                    ParentId  = null,
                    Name      = "GetFunctionsPaging_NoFilter_ReturnSuccess4",
                    SortOrder = 4,
                    Url       = "/test4"
                }
            });
            _context.SaveChangesAsync();

            _context.Commands.AddRange(new List <Command>()
            {
                new Command()
                {
                    Id = "TEST1", Name = "Xem"
                },
                new Command()
                {
                    Id = "TEST2", Name = "Thêm"
                },
                new Command()
                {
                    Id = "TEST3", Name = "Sửa"
                },
                new Command()
                {
                    Id = "TEST4", Name = "Xoá"
                },
                new Command()
                {
                    Id = "TEST5", Name = "Duyệt"
                },
            });

            var functions = _context.Functions;

            if (!_context.CommandInFunctions.Any())
            {
                foreach (var function in functions)
                {
                    var createAction = new CommandInFunction()
                    {
                        CommandId  = "TEST2",
                        FunctionId = function.Id
                    };
                    _context.CommandInFunctions.Add(createAction);

                    var updateAction = new CommandInFunction()
                    {
                        CommandId  = "TEST3",
                        FunctionId = function.Id
                    };
                    _context.CommandInFunctions.Add(updateAction);
                    var deleteAction = new CommandInFunction()
                    {
                        CommandId  = "TEST4",
                        FunctionId = function.Id
                    };
                    _context.CommandInFunctions.Add(deleteAction);

                    var viewAction = new CommandInFunction()
                    {
                        CommandId  = "TEST1",
                        FunctionId = function.Id
                    };
                    _context.CommandInFunctions.Add(viewAction);
                }
            }

            _context.SaveChangesAsync();
        }