public async Task <IActionResult> Submit([FromBody] Models.MeasureSubmissionViewModel data) { if (User.Identity.IsAuthenticated == false) { return(BadRequest(new Models.ApiErrorResult("User must be authenticated to view measures."))); } if (data == null) { return(BadRequest(new Models.ApiErrorResult("Invalid measure data."))); } if (!User.Claims.Any(cl => cl.Type == Identity.Claims.SubmitMeasure_Key)) { return(BadRequest(new Models.ApiErrorResult("The user does not have permission to submit measures."))); } List <string> errors = new List <string>(); if (!ValidateSubmission(data, errors)) { return(BadRequest(new { errors })); } var user = await _modelDB.Users.FindAsync(Guid.Parse(User.Claims.Where(x => x.Type == ClaimTypes.NameIdentifier).Select(x => x.Value).FirstOrDefault())); MeasurementMeta measure = new MeasurementMeta { CommonDataModel = data.CommonDataModel, DatabaseSystem = data.DatabaseSystem, DataSource = data.DataSource, DataSourceID = data.DataSourceID, DateRangeEnd = data.DateRangeEnd.Value, DateRangeStart = data.DateRangeStart.Value, MetricID = data.MetricID.Value, Network = data.Network, Organization = data.Organization, OrganizationID = data.OrganizationID, RunDate = data.RunDate.Value, SubmittedByID = user.ID, SubmittedOn = DateTime.UtcNow, CommonDataModelVersion = data.CommonDataModelVersion, ResultsDelimiter = data.ResultsDelimiter, ResultsTypeID = await _modelDB.MetricResultTypes.Where(rt => rt.Metrics.Any(m => m.ID == data.MetricID.Value)).Select(rt => rt.ID).FirstOrDefaultAsync(), SupportingResources = data.SupportingResources }; measure.Measurements = new HashSet <Measurement>(data.Measures.Select(i => new Measurement { MetadataID = measure.ID, RawValue = i.RawValue, Definition = string.IsNullOrEmpty(i.Definition) ? i.RawValue : i.Definition, Measure = i.Measure.HasValue ? i.Measure.Value : 0f, Total = i.Total }).ToArray()); _modelDB.MeasurementMeta.Add(measure); await _modelDB.SaveChangesAsync(); return(Ok()); }
/// <summary> /// Initiates the document object to be saved in the database /// </summary> /// <returns></returns> public virtual async Task <Document> SaveDocumentToDatabaseAsync(ModelDataContext dataContext, ChunkMetaData metadata, Guid itemID, Guid uploadByID) { var doc = new Document { Name = metadata.FileName, FileName = metadata.FileName, MimeType = FileEx.GetMimeTypeByExtension(metadata.FileName), ItemID = itemID, Length = metadata.TotalFileSize, UploadedByID = uploadByID, ChunkCount = metadata.TotalChunks }; dataContext.Documents.Add(doc); await dataContext.SaveChangesAsync(); _logger.LogInformation($"Saved the document metadata to the database for { metadata.FileName }, ID: { doc.ID.ToString("D") }."); return(doc); }