public async Task <IActionResult> PostFunction([FromBody] FunctionCreateRequest request) { _logger.LogInformation(_localizer["BeginFunction"]); var errMess = _returnString.ReturnString(_localizer["IdExisted"], request.Id); var dbFunction = await _context.Functions.FindAsync(request.Id); if (dbFunction != null) { return(BadRequest(new ApiBadRequestResponse(errMess)));//dung dependecy } var function = new Function() { Id = request.Id, NameTemp = request.NameTemp, ParentId = request.ParentId, SortOrder = request.SortOrder, Url = request.Url, Icon = request.Icon }; _context.Functions.Add(function); var result = await _context.SaveChangesAsync(); if (result > 0) { _logger.LogInformation(_localizer["EndPostFunctionSuccess"]); return(CreatedAtAction(nameof(GetById), new { id = function.Id }, request)); } _logger.LogInformation(_localizer["EndPostFunctionFail"]); return(BadRequest(new ApiBadRequestResponse(_localizer["CreateFunctionFail"]))); }
public async Task <IActionResult> PutFunction(string id, [FromBody] FunctionCreateRequest request) { //// GET FUNCTION WITH ID (KEY) var function = await _context.Functions.FindAsync(id); //// IF KEY IS NOT EXIST (FUNCTION IS NULL), RETURN STATUS 404 if (function == null) { return(NotFound(new ApiNotFoundResponse($"Cannot found function with id {id}"))); } //// GIVE INPUT DATA FOR EACH FIELD OF OBJECT WHICH NEED UPDATE INFOMATIONS function.Name = request.Name; function.ParentId = request.ParentId; function.SortOrder = request.SortOrder; function.Url = request.Url; //// UPDATE FUNCTION AND SAVE CHANGE INTO DATATABLE OF DATABASE _context.Functions.Update(function); var result = await _context.SaveChangesAsync(); //// IF RESULT AFTER UPDATE IS GREATER THAN 0, UPDATE SUCCESS, RETURN STATUS IS 204, ELSE RETURN STATUS IS 400 if (result > 0) { return(NoContent()); } return(BadRequest(new ApiBadRequestResponse("Create function 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> PostFunction([FromBody] FunctionCreateRequest request) { _logger.LogInformation("Begin postFunction API"); var dbFunction = await _context.Functions.FindAsync(request.Id); if (dbFunction != null) { return(BadRequest(new ApiBadRequestResponse($"Function with id {request.Id} is existed."))); } var function = new Function() { Id = request.Id, Name = request.Name, ParentId = request.ParentId, SortOrder = request.SortOrder, Url = request.Url, Icon = request.Icon }; _context.Functions.Add(function); var result = await _context.SaveChangesAsync(); if (result > 0) { _logger.LogInformation("End PostFunction API - Success"); return(CreatedAtAction(nameof(GetById), new { id = function.Id }, request)); } else { _logger.LogInformation("End PostFunction API - Failed"); return(BadRequest(new ApiBadRequestResponse("Create function is failed"))); } }
public FunctionCreateRequestValidatorTest() { request = new FunctionCreateRequest() { Id = "test6", ParentId = null, Name = "test6", SortOrder = 6, Url = "/test6" }; validator = new FunctionCreateRequestValidator(); }
public FunctionCreateRequestValidatorTest() { _request = new FunctionCreateRequest { Id = "test6", ParentId = null, NameTemp = "test6", SortOrder = 6, Url = "/test6" }; var mock = new Mock <IStringLocalizer <FunctionCreateRequest> >(); var localizedString = new LocalizedString("", ""); mock.Setup(_ => _[""]).Returns(localizedString); var localizer = mock.Object; _validator = new FunctionCreateRequestValidator(localizer); }
public async Task <IActionResult> PostFunction([FromBody] FunctionCreateRequest request) { var function = new Function() { Id = request.Id, Name = request.Name, ParentId = request.ParentId, SortOrder = request.SortOrder, Url = request.Url }; _context.Functions.Add(function); var result = await _context.SaveChangesAsync(); if (result > 0) { return(CreatedAtAction(nameof(GetById), new { id = function.Id }, request)); } return(BadRequest()); }
public async Task <IActionResult> PutFunction(string id, [FromBody] FunctionCreateRequest request) { var function = await _context.Functions.FindAsync(id); if (function == null) { return(NotFound()); } function.Name = request.Name; function.ParentId = request.ParentId; function.SortOrder = request.SortOrder; function.Url = request.Url; _context.Functions.Update(function); var result = await _context.SaveChangesAsync(); if (result > 0) { return(NoContent()); } return(BadRequest()); }
public async Task <IActionResult> PutFunction(string id, [FromBody] FunctionCreateRequest request) { var function = await _context.Functions.FindAsync(id); if (function == null) { return(NotFound(new ApiNotFoundResponse($"Cannot found function with id {id}"))); } function.Name = request.Name; function.ParentId = request.ParentId; function.SortOrder = request.SortOrder; function.Url = request.Url; function.Icon = request.Icon; _context.Functions.Update(function); var result = await _context.SaveChangesAsync(); if (result > 0) { return(NoContent()); } return(BadRequest(new ApiBadRequestResponse("Create function failed"))); }
public async Task <IActionResult> PutFunction(string id, [FromBody] FunctionCreateRequest request) { var function = await _context.Functions.FindAsync(id); if (function == null) { return(NotFound(new ApiNotFoundResponse(_returnString.ReturnString(_localizer["Cannotfoundfunction"], id)))); } function.NameTemp = request.NameTemp; function.ParentId = request.ParentId; function.SortOrder = request.SortOrder; function.Url = request.Url; function.Icon = request.Icon; _context.Functions.Update(function); var result = await _context.SaveChangesAsync(); if (result > 0) { return(NoContent()); } return(BadRequest(new ApiBadRequestResponse(_localizer["CreateFunctionFail"]))); }