Exemplo n.º 1
0
        private string SaveAgreementToStorage(AgreementStorage agreement)
        {
            var json = JsonConvert.SerializeObject(agreement, new JsonSerializerSettings()
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                DateFormatString      = "yyyy-MM-dd"
            });

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
            CloudBlobClient     blobClient     = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer  container      = blobClient.GetContainerReference("erasmus");

            container.CreateIfNotExists();
            CloudBlockBlob blockBlob = container.GetBlockBlobReference($"{agreement.Student.Id}_{Guid.NewGuid().ToString("D")}");

            blockBlob.UploadText(json);
            return(blockBlob.Uri.ToString());
        }
Exemplo n.º 2
0
        public void ValidateAndSaveStudentAgreement(ErasmusUser user, AgreementViewModel model)
        {
            using (var db = new ErasmusDbContext())
            {
                #region Validation
                var sourceUniversity = db.Universities.SingleOrDefault(x => x.Id == model.SelectedSourceUniversity);
                var targetUniversity = db.Universities.SingleOrDefault(x => x.Id == model.SelectedTargetUniversity);
                if (sourceUniversity == null)
                {
                    throw new ValidationException("Invalid source university");
                }
                if (targetUniversity == null)
                {
                    throw new ValidationException("Invalid target university");
                }

                var agreement = db.UniversityAgreements.SingleOrDefault(x =>
                                                                        x.SourceUniversityId == sourceUniversity.Id && x.TargetUniversityId == targetUniversity.Id);
                if (agreement == null)
                {
                    throw new ValidationException("Invalid selection, agreement for selected universities not found");
                }

                var sourceFaculty =
                    sourceUniversity.Faculties.SingleOrDefault(x => x.Id == model.SelectedSourceFaculty);
                var targetFaculty =
                    targetUniversity.Faculties.SingleOrDefault(x => x.Id == model.SelectedTargetFaculty);
                if (sourceFaculty == null)
                {
                    throw new ValidationException("Invalid source faculty");
                }
                if (targetFaculty == null)
                {
                    throw new ValidationException("Invalid target faculty");
                }

                var sourceFos =
                    sourceFaculty.StudyFields.SingleOrDefault(x => x.Id == model.SelectedSourceFieldOfStudy);
                var targetFos =
                    targetFaculty.StudyFields.SingleOrDefault(x => x.Id == model.SelectedTargetFieldOfStudy);
                if (sourceFos == null)
                {
                    throw new ValidationException("Invalid source field of study");
                }
                if (targetFos == null)
                {
                    throw new ValidationException("Invalid target field of study");
                }

                var sourceStudySubjects = sourceFos.StudySubjects.Where(x => model.SelectedSourceStudySubjects.Contains(x.Id)).ToList();
                var targetStudySubjects = targetFos.StudySubjects.Where(x => model.SelectedTargetStudySubjects.Contains(x.Id)).ToList();

                var srcSsIds = sourceStudySubjects.Select(x => x.Id).ToList();
                var trgSsIds = targetStudySubjects.Select(x => x.Id).ToList();

                /*if (!srcSsIds.All(model.SelectedSourceStudySubjects.Contains) || srcSsIds.Count != model.SelectedSourceStudySubjects.Count)
                 *   throw new ValidationException("Invalid source study subjects found");
                 * if (!trgSsIds.All(model.SelectedTargetStudySubjects.Contains) || trgSsIds.Count != model.SelectedTargetStudySubjects.Count)
                 *   throw new ValidationException("Invalid target study subjects found");*/

                var sourceCreditCount = sourceStudySubjects.Sum(x => x.Credits);
                var targetCreditCount = targetStudySubjects.Sum(x => x.Credits);
                if (sourceCreditCount != targetCreditCount)
                {
                    throw new ValidationException("Credit count must match");
                }

                if (model.From < DateTime.UtcNow)
                {
                    throw new ValidationException("Date from can't be earlier than today");
                }
                if (model.To < model.From)
                {
                    throw new ValidationException("Date to can't be earlier than date from");
                }

                //TODO add whatever else is needed. Semester, min credit count, language, time range etc.
                #endregion

                var storageModel = new AgreementStorage()
                {
                    Student             = user,
                    LanguageLevel       = model.LanguageLevel,
                    SourceUniversity    = sourceUniversity,
                    TargetUniversity    = targetUniversity,
                    Semester            = model.SelectedSemester,
                    Language            = model.Language,
                    TargetStudySubjects = targetStudySubjects,
                    EndDate             = model.To,
                    FinancingSource     = "",
                    Scholarship         = 0,
                    SourceFaculty       = sourceFaculty,
                    SourceFieldOfStudy  = sourceFos,
                    SourceStudySubjects = sourceStudySubjects,
                    StartDate           = model.From,
                    State              = "NEW",
                    StoragePath        = "",
                    TargetFaculty      = targetFaculty,
                    TargetFieldOfStudy = targetFos
                };
                var path = SaveAgreementToStorage(storageModel);
                db.Agreements.Add(new Agreement()
                {
                    EndDate              = model.To,
                    ErasmusUserId        = user.Id,
                    FinancingSource      = "",
                    Scholarship          = 0,
                    StartDate            = model.From,
                    State                = AgreementState.New,
                    StoragePath          = path,
                    StudyField           = sourceFos.Id,
                    SourceUniversityId   = sourceUniversity.Id,
                    TargetUniversityId   = targetUniversity.Id,
                    Language             = model.Language,
                    LanguageLevel        = model.LanguageLevel,
                    Semester             = model.SelectedSemester,
                    SourceFieldOfStudyId = model.SelectedSourceFieldOfStudy,
                    TargetFieldOfStudyId = model.SelectedTargetFieldOfStudy,
                    SourceFacultyId      = sourceFaculty.Id,
                    TargetFacultyId      = targetFaculty.Id,
                    SourceSubjects       = sourceStudySubjects,
                    TargetSubjects       = targetStudySubjects
                });
                db.SaveChanges();
            }
        }