public ActionResult <CohortRepresentation> Update(CohortAPI cohortApi) { // validate that session exists string sessionUserId; if (!Utils.ValidateSession(this.Request.Headers, out sessionUserId)) { return(BadRequest("Session does not exists.")); } Guid id = Utils.ToGuid(cohortApi.Id, false); if (id == Guid.Empty) { return(BadRequest("Invalid id (" + id + ")")); } // setup cohort Cohort cohort = new Cohort(); cohort.Id = cohortApi.Id; cohort.UserId = cohortApi.UserId; cohort.NumberOfSubjectsRequired = cohortApi.NumberOfSubjectsRequired; cohort.CohortType = cohortApi.CohortType; cohort.RequestAdmin = cohortApi.RequestAdmin; cohort.RequestUser = cohortApi.RequestUser; cohort.LastUpdate = DateTime.UtcNow; // update cohort Program.MedialynxData.cohortDBAPI.Update(cohort); // update/create/delete cohort links (neccessary enum items will be created with link) Program.MedialynxData.cohortEnumLinkDBAPI.UpdateLinks(cohort.Id, cohortApi.cohortEnumLinks); Program.MedialynxData.historyDBAPI.Add( new HistoryItem( sessionUserId, cohort.Id, this.GetType().ToString(), "Update cohort called with data: " + JsonSerializer.Serialize(cohortApi) ) ); // reload and alive cohort string sid = Utils.ToGuid(cohort.Id, false).ToString("B"); List <Cohort> cohorts = Program.MedialynxData.cohortDBAPI.Get(sid); if (cohorts.Count != 1) { return(NotFound()); } return(this.GetCohortRepresentation(cohorts[0])); }
public ActionResult <CohortRepresentation> Create(CohortAPI cohortApi) { // validate that session exists string sessionUserId; if (!Utils.ValidateSession(this.Request.Headers, out sessionUserId)) { return(BadRequest("Session does not exists.")); } // validate links foreach (var link in cohortApi.cohortEnumLinks) { bool enumItemIdDefined = link.EnumItemId != null && link.EnumItemId != ""; if (Utils.IsEmpty(link.enumItem) && !enumItemIdDefined) { return(BadRequest("enumItem and enumItemIdDefined can't be null at same time.")); } if (!Utils.IsEmpty(link.enumItem) && enumItemIdDefined) { return(BadRequest("enumItem and enumItemIdDefined can't be initialized at same time.")); } } // validate that cohort is not exists Cohort existsCohort = Program.MedialynxData.cohortDBAPI.GetFirstByUser(cohortApi.UserId); if (existsCohort != null) { return(BadRequest("Cohort already exists")); // cohort already exists. Please use update. } cohortApi.Id = Guid.NewGuid().ToString("B"); // setup cohort Cohort cohort = new Cohort(); cohort.Id = cohortApi.Id; cohort.UserId = cohortApi.UserId; cohort.NumberOfSubjectsRequired = cohortApi.NumberOfSubjectsRequired; cohort.CohortType = cohortApi.CohortType; cohort.RequestAdmin = cohortApi.RequestAdmin; cohort.RequestUser = cohortApi.RequestUser; cohort.CreationDate = DateTime.UtcNow; cohort.LastUpdate = cohort.CreationDate; // create cohort Program.MedialynxData.cohortDBAPI.Add(cohort); // create cohort links (necessary enum items will be created with link) Program.MedialynxData.cohortEnumLinkDBAPI.CreateLinks(cohort.Id, cohortApi.cohortEnumLinks); Program.MedialynxData.historyDBAPI.Add( new HistoryItem( sessionUserId, cohort.Id, this.GetType().ToString(), "Create cohort called with data: " + JsonSerializer.Serialize(cohortApi) ) ); // reload and alive cohort string sid = Utils.ToGuid(cohort.Id, false).ToString("B"); List <Cohort> cohorts = Program.MedialynxData.cohortDBAPI.Get(sid); if (cohorts.Count != 1) { return(NotFound()); } Notification notification = new Notification(); notification.Id = Guid.NewGuid().ToString("B"); notification.UserId = sessionUserId; notification.Message = "Cohort created"; notification.NotificationType = 0; notification.Status = NotificationStatus.Created; notification.CreationDate = DateTime.UtcNow; notification.LastUpdate = notification.CreationDate; Program.MedialynxData.notificationDBAPI.Add(notification); return(this.GetCohortRepresentation(cohorts[0])); }