public static void UpdateGroup(this Group group, GroupCreateUpdateModel groupVm) { group.ID = groupVm.ID; group.Name = groupVm.Name; group.Description = groupVm.Description; group.DelegateId = groupVm.DelegateId; group.StartDate = groupVm.StartDate; group.EndDate = groupVm.EndDate; }
public async Task <HttpResponseMessage> Create(HttpRequestMessage request, GroupCreateUpdateModel groupVM) { if (ModelState.IsValid) { var newGroup = new Group(); GroupCreateUpdateModel responseData = new GroupCreateUpdateModel(); newGroup.UpdateGroup(groupVM); if (_groupService.IsDuplicateGroup(groupVM.Name, groupVM.ID)) { return(request.CreateResponse(HttpStatusCode.BadRequest, Common.Constants.MessageSystem.GroupExist)); } else { if (_groupService.Add(newGroup) != null) { _groupService.SaveChange(); var groupId = _groupService.GetGroupByName(groupVM.Name).ID; var appUser = await AppUserManager.FindByIdAsync(groupVM.GroupLeadID); appUser.GroupId = groupId; var result = await AppUserManager.UpdateAsync(appUser); if (result.Succeeded) { return(request.CreateResponse(HttpStatusCode.Created, responseData)); } else { _groupService.Delete(groupId); return(request.CreateResponse(HttpStatusCode.BadRequest, responseData)); } } responseData = Mapper.Map <Group, GroupCreateUpdateModel>(newGroup); return(request.CreateResponse(HttpStatusCode.BadRequest, responseData)); } } else { return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } }
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)); } }
public async Task <HttpResponseMessage> Update(HttpRequestMessage request, GroupCreateUpdateModel groupVm) { if (!ModelState.IsValid) { return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } else { var newGroup = _groupService.GetGroupById(groupVm.ID.ToString()); if (_groupService.IsDuplicateGroup(groupVm.Name, groupVm.ID)) { return(request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.GroupExist)); } else { newGroup.UpdateGroup(groupVm); _groupService.Update(newGroup); _groupService.SaveChange(); var groupId = _groupService.GetGroupByName(groupVm.Name).ID; var oldLeader = await AppUserManager.FindByIdAsync(_groupService.GetGroupLeadIdByGroup(groupVm.ID)); var newLeader = await AppUserManager.FindByIdAsync(groupVm.GroupLeadID); if (oldLeader != null) { if (newLeader.Equals(oldLeader)) { return(request.CreateResponse(HttpStatusCode.Created, newGroup)); } else { newLeader.GroupId = groupId; oldLeader.GroupId = null; var resultUpdateNewLeader = await AppUserManager.UpdateAsync(newLeader); var resultUpdateOldLeader = await AppUserManager.UpdateAsync(oldLeader); if (resultUpdateNewLeader.Succeeded && resultUpdateOldLeader.Succeeded) { return(request.CreateResponse(HttpStatusCode.Created, newGroup)); } else { return(request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_UPDATE_LEADER)); } } } else { newLeader.GroupId = groupId; var resultUpdateNewLeader = await AppUserManager.UpdateAsync(newLeader); if (resultUpdateNewLeader.Succeeded) { return(request.CreateResponse(HttpStatusCode.Created, newGroup)); } else { return(request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_UPDATE_LEADER)); } } } } }