public void Add(CohortEnumLink link) { using (var dbContext = new MedialynxDbCohortEnumLinkContext()) { dbContext.CohortEnumLink.Add(link); dbContext.SaveChanges(); } }
public ActionResult <CohortEnumLink> Create(CohortEnumLink link) { // validate that session exists string sessionUserId; if (!Utils.ValidateSession(this.Request.Headers, out sessionUserId)) { return(BadRequest("Session does not exists.")); } Guid id = Utils.ToGuid(link.Id, false); link.CreationDate = DateTime.UtcNow; link.LastUpdate = link.CreationDate; if (id == Guid.Empty) { link.Id = Guid.NewGuid().ToString("B"); Program.MedialynxData.cohortEnumLinkDBAPI.Add(link); Program.MedialynxData.historyDBAPI.Add( new HistoryItem( sessionUserId, link.Id, this.GetType().ToString(), "Create CohortEnumLink called with data: " + JsonSerializer.Serialize(link) ) ); return(CreatedAtAction(nameof(GetById), new { id = link.Id }, link)); } return(BadRequest("Cohort enum link object can't be created. Please check id. (" + link.Id + ")")); }
public void Update(CohortEnumLink link) { using (var dbContext = new MedialynxDbCohortEnumLinkContext()) { CohortEnumLink existsLink = dbContext.CohortEnumLink.FirstOrDefault(l => l != null && l.Id == link.Id); if (existsLink != null) { if (Utils.CopyPropertyValues <CohortEnumLink>(link, existsLink)) { dbContext.CohortEnumLink.Update(existsLink); dbContext.SaveChanges(); } } } }
private void CreateNewLink(string cohortId, CohortEnumLinkAPI linkAPI) { CohortEnumLink link = new CohortEnumLink(); link.Id = Guid.NewGuid().ToString("B"); link.CohortId = cohortId; link.CohortEnumId = linkAPI.CohortEnumId; link.EnumItemId = linkAPI.EnumItemId; link.Include = linkAPI.Include; link.Percentage = linkAPI.Percentage; link.CreationDate = DateTime.UtcNow; link.LastUpdate = link.CreationDate; this.Add(link); }
internal List <CohortEnumLink> ExistingOptionsAsLink <T>(List <T> items, string cohortId, string cohortEnumId) { List <CohortEnumLink> links = new List <CohortEnumLink>(); foreach (var item in items) { var Id = item.GetType().GetProperty("Id").GetValue(item, null); CohortEnumLink newEnumLink = new CohortEnumLink(); newEnumLink.Id = Guid.NewGuid().ToString("B"); newEnumLink.CohortId = cohortId; newEnumLink.CohortEnumId = cohortEnumId; newEnumLink.EnumItemId = (string)Id; newEnumLink.CreationDate = DateTime.UtcNow; newEnumLink.LastUpdate = newEnumLink.CreationDate; links.Add(newEnumLink); } return(links); }
public List <CohortEnumLink> Get(string linkId = "{00000000-0000-0000-0000-000000000000}") { Guid id = Utils.ToGuid(linkId); List <CohortEnumLink> links = new List <CohortEnumLink>(); using (var dbContext = new MedialynxDbCohortEnumLinkContext()) { if (id != Guid.Empty) { string sid = id.ToString("B"); CohortEnumLink link = dbContext.CohortEnumLink.FirstOrDefault(link => link != null && link.Id == sid); links.Add(link); } else { // Add all links links.AddRange(dbContext.CohortEnumLink); } } return(links); }
public void UpdateLinks(string cohortId, List <CohortEnumLinkAPI> LinksForUpdate) { if (LinksForUpdate == null) { return; // nothing to update } // collect id's for update List <string> updatesIds = new List <string>(); foreach (CohortEnumLinkAPI linkAPI in LinksForUpdate) { Guid id = Utils.ToGuid(linkAPI.Id, false); if (id != Guid.Empty) // empty = create { updatesIds.Add(linkAPI.Id); } } // exists guids List <string> existsIds = new List <string>(); using (var dbContext = new MedialynxDbCohortEnumLinkContext()) { List <CohortEnumLink> existsLinks = dbContext.CohortEnumLink.Where(link => link.CohortId == cohortId).ToList(); foreach (CohortEnumLink link in existsLinks) { existsIds.Add(link.Id); } } // check that exists id present in new links collection. List <string> toDeleteIds = new List <string>(); foreach (string item in existsIds) { if (!updatesIds.Contains(item)) { toDeleteIds.Add(item); } } // remove not exists limks this.RemoveRange(toDeleteIds); foreach (CohortEnumLinkAPI linkAPI in LinksForUpdate) { // enum item defined within link item. Must be created and used instead linkAPI.EnumItemId if (linkAPI.enumItem != null) { this.CreateEnumItem(linkAPI); } List <CohortEnumLink> existLinks = this.Get(linkAPI.Id); CohortEnumLink existLink = existLinks.Count > 0 ? existLinks[0] : null; if (existLink == null) { this.CreateNewLink(cohortId, linkAPI); } else // update exists link required { CohortEnumLink link = new CohortEnumLink(); link.Id = Guid.NewGuid().ToString("B"); link.CohortId = cohortId; link.CohortEnumId = linkAPI.CohortEnumId; link.EnumItemId = linkAPI.EnumItemId; link.Include = linkAPI.Include; link.Percentage = linkAPI.Percentage; link.LastUpdate = link.CreationDate; this.Update(link); } } }