public IHttpActionResult PostOrganizationGTICreateLink(OrganizationGTICreateLinkDTO links) { if (links == null) { return(BadRequest(ModelState)); } if (links.OrganizationGTIIds == null) { return(BadRequest()); } // string userId = ActionContext.RequestContext.Principal.Identity.GetUserId(); // ApplicationUser user = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(userId); // ApplicationUser user = identityHelper.FindUserById(User.Identity.GetUserId()); if (identityHelper.GetUserTableName(User) == "Employee") { UnitOfWork unitOfWork = new UnitOfWork(factory); foreach (var item in links.OrganizationGTIIds) { OrganizationGTILink existingLink = unitOfWork.OrganizationGTILinksRepository .Get(d => d.Deleted != true && d.GTIId == item) .FirstOrDefault(); if (existingLink != null) { return(BadRequest(String.Format("Link to {0} already exist", unitOfWork.OrganizationGTIsRepository.GetByID(item).NativeName))); } } List <OrganizationGTILinkDTO> createdLinks = new List <OrganizationGTILinkDTO>(); int EmployeeId = identityHelper.GetUserTableId(User); int OrganzationId = links.OrganizationId; try { foreach (var item in links.OrganizationGTIIds) { //Добавление int OrganizationGTIId = item; OrganizationGTILink link = new OrganizationGTILink(); link.Id = link.NewId(unitOfWork); link.OrganizationId = OrganzationId; link.GTIId = OrganizationGTIId; link.EmployeeId = EmployeeId; unitOfWork.OrganizationGTILinksRepository.Insert(link); unitOfWork.Save(); //Достать со всеми вложенными OrganizationGTILinkDTO newLink = unitOfWork.OrganizationGTILinksRepository .Get(d => d.Id == link.Id).FirstOrDefault().ToDTO(); if (link.GTIId != null) { newLink.OrganizationGTI = unitOfWork.OrganizationGTIsRepository .Get(d => d.Id == newLink.GTIId).FirstOrDefault().ToDTO(); if (newLink.OrganizationGTI != null) { newLink.OrganizationGTI.Office = unitOfWork.OfficesRepository .Get(d => d.Id == newLink.OrganizationGTI.OfficeId).FirstOrDefault().ToDTO(); } } createdLinks.Add(newLink); } } catch (NotFoundException nfe) { return(NotFound()); } catch (ConflictException ce) { return(Conflict()); } catch (Exception e) { return(BadRequest(e.Message)); } return(Ok(createdLinks)); } return(BadRequest()); }
public IHttpActionResult PostOrganizationGTILink(OrganizationGTILinkDTO organizationGTILink) { try { if (organizationGTILink == null) { return(BadRequest(ModelState)); } if (identityHelper.GetUserTableName(User) == "Employee") { OrganizationGTILink link = organizationGTILink.FromDTO(); int EmployeeId = identityHelper.GetUserTableId(User); link.EmployeeId = EmployeeId; UnitOfWork unitOfWork = new UnitOfWork(factory); OrganizationGTILink existingLink = unitOfWork.OrganizationGTILinksRepository .Get(d => d.Deleted != true && d.GTIId == link.GTIId) .FirstOrDefault(); if (existingLink != null) { return(BadRequest("Link to this GTI Organization already exist")); } link.Id = link.NewId(unitOfWork); unitOfWork.OrganizationGTILinksRepository.Insert(link); unitOfWork.Save(); OrganizationGTILinkDTO dto = unitOfWork.OrganizationGTILinksRepository .Get(d => d.Id == link.Id).FirstOrDefault().ToDTO(); if (dto.GTIId != null) { dto.OrganizationGTI = unitOfWork.OrganizationGTIsRepository .Get(d => d.Id == dto.GTIId).FirstOrDefault().ToDTO(); if (dto.OrganizationGTI != null) { dto.OrganizationGTI.Office = unitOfWork.OfficesRepository .Get(d => d.Id == dto.OrganizationGTI.OfficeId).FirstOrDefault().ToDTO(); } } return(CreatedAtRoute("GetOrganizationGTILink", new { id = dto.Id }, dto)); } else { return(BadRequest()); } } catch (NotFoundException nfe) { return(NotFound()); } catch (ConflictException ce) { return(Conflict()); } catch (Exception e) { return(BadRequest(e.Message)); } }