/// <summary>
        /// maps enrichment data from the data object to the returned enrichment model
        /// </summary>
        /// <param name="source">enrichment data object</param>
        /// <returns>enrichment model with enrichment data (if found). Null if there is no source record</returns>
        public UcasCourseEnrichmentGetModel Convert(CourseEnrichment source)
        {
            if (source == null)
            {
                return(null);
            }

            var enrichmentToReturn = new UcasCourseEnrichmentGetModel();
            var enrichmentModel    = source.JsonData != null?JsonConvert.DeserializeObject <CourseEnrichmentModel>(source.JsonData, _jsonSerializerSettings) : null;

            enrichmentToReturn.EnrichmentModel           = enrichmentModel;
            enrichmentToReturn.CreatedTimestampUtc       = source.CreatedAt;
            enrichmentToReturn.UpdatedTimestampUtc       = source.UpdatedAt;
            enrichmentToReturn.CreatedByUserId           = source.CreatedByUser?.Id ?? 0;
            enrichmentToReturn.UpdatedByUserId           = source.UpdatedByUser?.Id ?? 0;
            enrichmentToReturn.LastPublishedTimestampUtc = source.LastPublishedTimestampUtc;
            enrichmentToReturn.Status       = source.Status;
            enrichmentToReturn.ProviderCode = source.ProviderCode;
            enrichmentToReturn.CourseCode   = source.UcasCourseCode;
            return(enrichmentToReturn);
        }
Exemplo n.º 2
0
        /// <summary>
        /// This is an upsert.
        /// If a draft record exists then update it.
        /// If a draft record does not exist then add a new one.
        /// If a new draft record is created then:
        ///check for the existence of a previous published record and get the last pulished date
        /// </summary>
        /// <param name="model">holds the enriched data</param>
        /// <param name="providerCode">the provider code for the enrichment data</param>
        /// <param name="ucasCourseCode">the course code for the enrichment data</param>
        /// <param name="email">the email of the user</param>
        public void SaveCourseEnrichment(CourseEnrichmentModel model, string providerCode, string ucasCourseCode, string email)
        {
            var userOrg = ValidateUserOrg(email, providerCode);

            providerCode   = providerCode.ToUpperInvariant();
            ucasCourseCode = ucasCourseCode.ToUpperInvariant();

            var course = _context.Courses
                         .Include(c => c.CourseEnrichments)
                         .SingleOrDefault(c =>
                                          c.Provider.ProviderCode == providerCode &&
                                          c.Provider.RecruitmentCycle.Year == RecruitmentCycle.CurrentYear &&
                                          c.CourseCode == ucasCourseCode);

            if (course == null)
            {
                throw new Exception($"Course not found {RecruitmentCycle.CurrentYear}/{providerCode}/{ucasCourseCode}");
            }

            var enrichmentDraftRecord = course.CourseEnrichments
                                        .Where(ce => ce.Status == EnumStatus.Draft)
                                        .OrderByDescending(x => x.Id)
                                        .FirstOrDefault();

            string content = _converter.ConvertToJson(model);

            if (enrichmentDraftRecord != null)
            {
                //update
                enrichmentDraftRecord.UpdatedAt     = DateTime.UtcNow;
                enrichmentDraftRecord.UpdatedByUser = userOrg.User;
                enrichmentDraftRecord.JsonData      = content;
            }
            else
            {
                //insert
                var enrichmentPublishRecord = course.CourseEnrichments
                                              .Where(ce => ce.Status == EnumStatus.Published)
                                              .OrderByDescending(x => x.Id)
                                              .FirstOrDefault();

                DateTime?lastPublishedDate = null;
                if (enrichmentPublishRecord != null)
                {
                    lastPublishedDate = enrichmentPublishRecord.LastPublishedTimestampUtc;
                }
                var enrichment = new CourseEnrichment
                {
                    ProviderCode              = userOrg.UcasProviderCode,
                    UcasCourseCode            = ucasCourseCode,
                    CreatedAt                 = DateTime.UtcNow,
                    UpdatedAt                 = DateTime.UtcNow,
                    LastPublishedTimestampUtc = lastPublishedDate,
                    CreatedByUser             = userOrg.User,
                    UpdatedByUser             = userOrg.User,
                    Status   = EnumStatus.Draft,
                    JsonData = content,
                };
                course.CourseEnrichments.Add(enrichment);
            }
            _context.Save();
        }