コード例 #1
0
ファイル: GroupController.cs プロジェクト: hanuoc/tenatms
        public async Task <HttpResponseMessage> SetDelegateDefault(HttpRequestMessage request, string userId, string groupId, GroupCreateUpdateModel groupVm)
        {
            if (!ModelState.IsValid)
            {
                return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
            else
            {
                List <string>             lstRequestId            = new List <string>();
                List <string>             lstExplanationRequestId = new List <string>();
                List <object>             lstData               = new List <object>();
                List <Request>            lstRequest            = new List <Request>();
                List <ExplanationRequest> lstExplanationRequest = new List <ExplanationRequest>();
                var ChangeStatusById = AppUserManager.FindByNameAsync(User.Identity.Name).Result.Id;
                var newGroup         = _groupService.GetGroupById(groupVm.ID.ToString());
                newGroup.UpdateGroup(groupVm);
                _groupService.Update(newGroup);
                _groupService.SaveChange();

                //Get request by user and group.Filter by StatusRequest is Pendding
                var model = _requestService.GetAllRequestByUser(userId, groupId);
                var group = _groupService.GetGroupById(groupId);
                var data  = model.Where(x => x.StatusRequest.Name.Equals(CommonConstants.StatusPending));
                if (group.DelegateId != null)
                {
                    foreach (var item in data)
                    {
                        if (item.CreatedDate != null)
                        {
                            if (group.StartDate <= item.CreatedDate.Value.Date && group.EndDate >= item.CreatedDate.Value.Date)
                            {
                                lstRequestId.Add(item.ID.ToString());
                                lstRequest.Add(item);
                            }
                        }
                    }
                    lstData.AddRange(lstRequest);
                    //Change status request by delegate default
                    _requestService.ChangeStatusDelegateDefault(CommonConstants.StatusDelegation, newGroup.DelegateId, newGroup.StartDate.Value, newGroup.EndDate.Value, ChangeStatusById, lstRequestId.ToArray());
                }


                //Get explanation by userid and groupid.Filter by StatusRequest is Pending
                var explanation = _explanationRequestService.GetListExplanationByUser(userId, groupId).Where(a => a.StatusRequest.Name.Equals(CommonConstants.StatusPending));

                if (explanation.Count() > 0 && group.DelegateId != null)
                {
                    foreach (var explanationItem in explanation)
                    {
                        if (explanationItem.CreatedDate != null)
                        {
                            if (group.StartDate <= explanationItem.CreatedDate.Value.Date && group.EndDate >= explanationItem.CreatedDate.Value.Date)
                            {
                                lstExplanationRequest.Add(explanationItem);
                                lstExplanationRequestId.Add(explanationItem.ID.ToString());
                            }
                        }
                    }
                    lstData.AddRange(lstExplanationRequest);
                    //Change status explanation request by delegate default
                    _explanationRequestService.ChangeStatusExplanationDelegateDefault(lstExplanationRequestId.ToArray(), CommonConstants.StatusDelegation, newGroup.DelegateId, newGroup.StartDate.Value, newGroup.EndDate.Value);
                }

                return(request.CreateResponse(HttpStatusCode.Created, lstData));
            }
        }