コード例 #1
0
 public HttpResponseMessage Create(HttpRequestMessage request, FunctionViewModel functionViewModel)
 {
     if (ModelState.IsValid)
     {
         var model = new Function();
         try
         {
             bool check = _functionService.CheckExistedId(functionViewModel.ID);
             if (check == true)
             {
                 return(request.CreateErrorResponse(HttpStatusCode.BadRequest, "Dữ liệu đã tồn tại"));
             }
             if (functionViewModel.ParentId == "")
             {
                 functionViewModel.ParentId = null;
             }
             model.UpdateFunction(functionViewModel);
             _functionService.Create(model);
             _functionService.Save();
             return(request.CreateResponse(HttpStatusCode.OK, functionViewModel));
         }
         catch (Exception ex)
         {
             return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
         }
     }
     else
     {
         return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
     }
 }
コード例 #2
0
        public HttpResponseMessage Create(HttpRequestMessage request, FunctionViewModel functionVm)
        {
            return(CreateHttpResponse(request, () =>
            {
                HttpResponseMessage response = null;
                if (!ModelState.IsValid)
                {
                    response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
                }
                else
                {
                    var newFunction = new Function();
                    if (_functionService.CheckExistedId(functionVm.ID))
                    {
                        return request.CreateErrorResponse(HttpStatusCode.BadRequest, "Id đã tồn tại");
                    }
                    else
                    {
                        if (functionVm.ParentId == "")
                        {
                            functionVm.ParentId = null;
                        }
                        newFunction.UpdateFunction(functionVm);
                        _functionService.Create(newFunction);
                        _functionService.Save();
                        response = request.CreateResponse(HttpStatusCode.OK, functionVm);
                    }
                }

                return response;
            }));
        }
コード例 #3
0
        public HttpResponseMessage Create(HttpRequestMessage request, FunctionViewModel functionViewModel)
        {
            if (ModelState.IsValid)
            {
                var newFunction = new Function();
                try
                {
                    if (_functionService.CheckExistedId(functionViewModel.ID))
                    {
                        return(request.CreateErrorResponse(HttpStatusCode.BadRequest, "ID đã tồn tại"));
                    }
                    else
                    {
                        if (functionViewModel.ParentId == "")
                        {
                            functionViewModel.ParentId = null;
                        }
                        newFunction.UpdateFunction(functionViewModel);

                        _functionService.Create(newFunction);
                        _functionService.Save();
                        return(request.CreateResponse(HttpStatusCode.OK, functionViewModel));
                    }
                }
                catch (Exception dex)
                {
                    return(request.CreateErrorResponse(HttpStatusCode.BadRequest, dex.Message));
                }
            }
            else
            {
                return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
コード例 #4
0
        public HttpResponseMessage Create(HttpRequestMessage request, FunctionViewModel functionViewModel)
        {
            Func <HttpResponseMessage> func = () =>
            {
                HttpResponseMessage response = null;
                if (ModelState.IsValid)
                {
                    Function newFunctionDb = new Function();
                    if (_functionService.CheckExistedId(functionViewModel.ID))
                    {
                        response = request.CreateErrorResponse(HttpStatusCode.BadRequest, "ID đã tồn tại");
                    }
                    else
                    {
                        if (functionViewModel.ParentId == "")
                        {
                            functionViewModel.ParentId = null;
                        }
                        newFunctionDb.UpdateFunction(functionViewModel);
                        _functionService.Create(newFunctionDb);
                        _functionService.SaveChange();
                        response = request.CreateResponse(HttpStatusCode.Created, functionViewModel);
                    }
                }
                return(response);
            };

            return(CreateHttpResponse(request, func));
        }
 public async Task <IActionResult> SaveEntity(FunctionViewModel functionViewModel)
 {
     if (!ModelState.IsValid)
     {
         var allErrors = ModelState.Values.SelectMany(v => v.Errors);
         return(new BadRequestObjectResult(allErrors));
     }
     else
     {
         if (!_functionService.CheckExistedId(functionViewModel.Id))
         {
             var notificationId = Guid.NewGuid().ToString();
             var announcement   = new AnnouncementViewModel
             {
                 Title       = User.GetSpecificClaim("FullName"),
                 DateCreated = DateTime.Now,
                 Content     = $"Function {functionViewModel.Name} has been created",
                 Id          = notificationId,
                 UserId      = User.GetUserId(),
                 Image       = User.GetSpecificClaim("Avatar"),
                 Status      = Status.Active
             };
             var announcementUsers = new List <AnnouncementUserViewModel>()
             {
                 new AnnouncementUserViewModel()
                 {
                     AnnouncementId = notificationId, HasRead = false, UserId = User.GetUserId()
                 }
             };
             _functionService.Add(announcement, announcementUsers, functionViewModel);
             await _hubContext.Clients.All.SendAsync("ReceiveMessage", announcement);
         }
         else
         {
             var notificationId = Guid.NewGuid().ToString();
             var announcement   = new AnnouncementViewModel
             {
                 Title       = User.GetSpecificClaim("FullName"),
                 DateCreated = DateTime.Now,
                 Content     = $"Function {functionViewModel.Name} has been updated",
                 Id          = notificationId,
                 UserId      = User.GetUserId(),
                 Image       = User.GetSpecificClaim("Avatar"),
                 Status      = Status.Active
             };
             var announcementUsers = new List <AnnouncementUserViewModel>()
             {
                 new AnnouncementUserViewModel()
                 {
                     AnnouncementId = notificationId, HasRead = false, UserId = User.GetUserId()
                 }
             };
             _functionService.Update(announcement, announcementUsers, functionViewModel);
             await _hubContext.Clients.All.SendAsync("ReceiveMessage", announcement);
         }
         _functionService.Save();
         return(new OkObjectResult(functionViewModel));
     }
 }
コード例 #6
0
        public HttpResponseMessage Create(HttpRequestMessage request, FunctionViewModel functionViewModel)
        {
            if (ModelState.IsValid)
            {
                var newFunction            = new Function();
                var identity               = (ClaimsIdentity)User.Identity;
                IEnumerable <Claim> claims = identity.Claims;
                try
                {
                    if (_functionService.CheckExistedId(functionViewModel.ID))
                    {
                        return(request.CreateErrorResponse(HttpStatusCode.BadRequest, "ID đã tồn tại"));
                    }
                    else
                    {
                        if (functionViewModel.ParentId == "")
                        {
                            functionViewModel.ParentId = null;
                        }
                        newFunction.UpdateFunction(functionViewModel);

                        _functionService.Create(newFunction);
                        _functionService.Save();
                        Log log = new Log()
                        {
                            AppUserId = claims.FirstOrDefault().Value,
                            Content   = Notification.CREATE_FUNCTION,
                            Created   = DateTime.Now
                        };
                        _logService.Create(log);
                        _logService.Save();
                        return(request.CreateResponse(HttpStatusCode.OK, functionViewModel));
                    }
                }
                catch (Exception dex)
                {
                    return(request.CreateErrorResponse(HttpStatusCode.BadRequest, dex.Message));
                }
            }
            else
            {
                return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
コード例 #7
0
 public IActionResult SaveEntity(FunctionViewModel functionViewModel)
 {
     if (!ModelState.IsValid)
     {
         var allErrors = ModelState.Values.SelectMany(v => v.Errors);
         return(new BadRequestObjectResult(allErrors));
     }
     else
     {
         if (!_functionService.CheckExistedId(functionViewModel.Id))
         {
             _functionService.Add(functionViewModel);
         }
         else
         {
             _functionService.Update(functionViewModel);
         }
         _functionService.Save();
         return(new OkObjectResult(functionViewModel));
     }
 }