public static Result <Subject> Create(string title, string excerpt, string[] concepts) { if (title.Length > 200) { return(Result.Fail <Subject>($"Tamanho máximo do título do assunto é de 200 caracteres. ({title})")); } var newSubject = new Subject(title, excerpt); var clist = Concept.GetConcepts(concepts); if (clist.IsFailure) { return(Result.Fail <Subject>(clist.Error)); } newSubject.Concepts = clist.Data; return(Result.Ok(newSubject)); }
public async Task <Result <List <ContractSubject> > > Handle(Contract request, CancellationToken cancellationToken) { if (request.UserRole == "Secretary") { return(Result.Fail <List <ContractSubject> >("Acesso Negado")); } var userId = ObjectId.Parse(request.UserId); var modId = ObjectId.Parse(request.ModuleId); var module = await _db.ModuleDraftCollection.AsQueryable() .Where(x => ( x.DeletedAt == null || x.DeletedAt == DateTimeOffset.MinValue ) && !x.DraftPublished && ( x.Id == modId || x.ModuleId == modId ) ) .FirstOrDefaultAsync(cancellationToken: cancellationToken); if (module == null) { var originalModule = await GetModule(request.ModuleId); if (originalModule == null) { return(Result.Fail <List <ContractSubject> >("Módulo não existe")); } module = await CreateModuleDraft( request, originalModule, cancellationToken ); } if (request.Subjects != null) { var oldValues = JsonConvert.SerializeObject(new List <ModuleDraft> { module }); if (request.DeleteNonExistent) { var existingIds = from o in request.Subjects where !string.IsNullOrEmpty(o.Id) select ObjectId.Parse(o.Id); var include = from c in module.Subjects where existingIds.Contains(c.Id) select c; module.Subjects = include.ToList(); } foreach (var csubject in request.Subjects) { Subject subject = null; if (string.IsNullOrEmpty(csubject.Id)) { var subjectResult = Subject.Create(csubject.Title, csubject.Excerpt, csubject.Concepts); if (subjectResult.IsFailure) { return(Result.Fail <List <ContractSubject> >(subjectResult.Error)); } subject = subjectResult.Data; subject.UpdatedAt = DateTimeOffset.UtcNow; module.Subjects.Add(subject); } else { subject = module.Subjects.FirstOrDefault(x => x.Id == ObjectId.Parse(csubject.Id)); if (subject == null) { return(Result.Fail <List <ContractSubject> >($"Assunto não encontrado ({csubject.Id} - {csubject.Title})")); } subject.Title = csubject.Title; subject.Excerpt = csubject.Excerpt; var clist = Concept.GetConcepts(csubject.Concepts); if (clist.IsFailure) { return(Result.Fail <List <ContractSubject> >(clist.Error)); } subject.Concepts = clist.Data; } if (csubject.UserProgresses != null) { subject.UserProgresses = new List <UserProgress>(); foreach (var crequirements in csubject.UserProgresses) { var req = UserProgress.Create(ProgressType.SubjectProgress, crequirements.Level, crequirements.Percentage); if (req.IsFailure) { return(Result.Fail <List <ContractSubject> >(req.Error)); } subject.UserProgresses.Add(req.Data); } } } var newDraftList = new List <ModuleDraft> { module }; var changeLog = AuditLog.Create(userId, module.Id, module.GetType().ToString(), JsonConvert.SerializeObject(newDraftList), EntityAction.Update, oldValues); await _db.AuditLogCollection.InsertOneAsync(changeLog); } await _db.ModuleDraftCollection.ReplaceOneAsync( t => t.Id == module.Id, module, cancellationToken : cancellationToken ); var response = module.Subjects.Select(subject => new ContractSubject() { Concepts = subject.Concepts.Select(c => c.Name).ToArray(), Excerpt = subject.Excerpt, Id = subject.Id.ToString(), Title = subject.Title, Contents = subject.Contents.Select(c => new ContentItem() { Duration = c.Duration, Excerpt = c.Excerpt, Id = c.Id, ReferenceUrls = c.ReferenceUrls, Title = c.Title, Type = c.Type, Value = c.Value, Concepts = c.Concepts.Select(concept => new ConceptItem() { Anchors = concept.Anchors, Positions = concept.Positions, Name = concept.Name }).ToArray(), }).ToList(), UserProgresses = subject.UserProgresses.Select(up => new ContractUserProgress { Level = up.Level, Percentage = up.Percentage }).ToList() } ).ToList(); return(Result.Ok(response)); }
public async Task <Result <List <ContractSubject> > > Handle(Contract request, CancellationToken cancellationToken) { if (request.UserRole == "Secretary") { return(Result.Fail <List <ContractSubject> >("Acesso Negado")); } var mId = ObjectId.Parse(request.ModuleId); var module = await(await _db .Database .GetCollection <Module>("Modules") .FindAsync(x => x.Id == mId, cancellationToken: cancellationToken)) .FirstOrDefaultAsync(cancellationToken: cancellationToken); if (module == null) { return(Result.Fail <List <ContractSubject> >("Módulo não Encontrado")); } if (request.UserRole == "Student") { var userId = ObjectId.Parse(request.UserId); if (!Module.IsInstructor(module, userId).Data) { return(Result.Fail <List <ContractSubject> >("Acesso Negado")); } } if (request.DeleteNonExistent) { var existingIds = from o in request.Subjects where !string.IsNullOrEmpty(o.Id) select ObjectId.Parse(o.Id); // deixando apenas os subjects que vieram na coleção var include = from c in module.Subjects where existingIds.Contains(c.Id) select c; module.Subjects = include.ToList(); } //Criando os assuntos foreach (var csubject in request.Subjects) { Subject subject = null; if (string.IsNullOrEmpty(csubject.Id)) { var subjectResult = Subject.Create(csubject.Title, csubject.Excerpt, csubject.Concepts); if (subjectResult.IsFailure) { return(Result.Fail <List <ContractSubject> >(subjectResult.Error)); } subject = subjectResult.Data; subject.UpdatedAt = DateTimeOffset.UtcNow; module.Subjects.Add(subject); } else { subject = module.Subjects.FirstOrDefault(x => x.Id == ObjectId.Parse(csubject.Id)); if (subject == null) { return(Result.Fail <List <ContractSubject> >($"Assunto não encontrado ({csubject.Id} - {csubject.Title})")); } subject.Title = csubject.Title; subject.Excerpt = csubject.Excerpt; var clist = Concept.GetConcepts(csubject.Concepts); if (clist.IsFailure) { return(Result.Fail <List <ContractSubject> >(clist.Error)); } subject.Concepts = clist.Data; } if (csubject.UserProgresses != null) { subject.UserProgresses = new List <UserProgress>(); foreach (var crequirements in csubject.UserProgresses) { var req = UserProgress.Create(ProgressType.SubjectProgress, crequirements.Level, crequirements.Percentage); if (req.IsFailure) { return(Result.Fail <List <ContractSubject> >(req.Error)); } subject.UserProgresses.Add(req.Data); } } } await _db.ModuleCollection.ReplaceOneAsync(t => t.Id == module.Id, module, cancellationToken : cancellationToken); var response = module.Subjects.Select(subject => new ContractSubject() { Concepts = subject.Concepts.Select(c => c.Name).ToArray(), Excerpt = subject.Excerpt, Id = subject.Id.ToString(), Title = subject.Title, Contents = subject.Contents.Select(c => new ContentItem() { Duration = c.Duration, Excerpt = c.Excerpt, Id = c.Id, ReferenceUrls = c.ReferenceUrls, Title = c.Title, Type = c.Type, Value = c.Value, Concepts = c.Concepts.Select(concept => new ConceptItem() { Anchors = concept.Anchors, Positions = concept.Positions, Name = concept.Name }).ToArray(), }).ToList(), UserProgresses = subject.UserProgresses.Select(up => new ContractUserProgress { Level = up.Level, Percentage = up.Percentage }).ToList() } ).ToList(); return(Result.Ok(response)); }