Ejemplo n.º 1
0
            public async Task <Result <ModuleDraft> > Handle(Contract request, CancellationToken cancellationToken)
            {
                try
                {
                    if (request.UserRole != "Admin" && request.UserRole != "Author")
                    {
                        return(Result.Fail <ModuleDraft>("Acesso Negado"));
                    }

                    var tags = new List <Tag>();
                    foreach (var tagStr in request.Tags)
                    {
                        var tag = Tag.Create(tagStr);
                        tags.Add(tag.Data);
                    }

                    var tutorsIds = request.TutorsIds != null?
                                    request.TutorsIds.Select(x => ObjectId.Parse(x)).ToList() :
                                        new List <ObjectId>();

                    var instructorId = String.IsNullOrEmpty(request.InstructorId) ?
                                       ObjectId.Empty : ObjectId.Parse(request.InstructorId);

                    Module          dbModule          = null;
                    Module          dbModuleClone     = null;
                    List <Question> dbModuleQuestions = new List <Question>();

                    if (!String.IsNullOrEmpty(request.ModuleId))
                    {
                        dbModule = await GetModule(request.ModuleId);
                    }

                    dbModuleClone = Module.Create(
                        request.Title,
                        dbModule.Excerpt,
                        dbModule.Instructor,
                        dbModule.ImageUrl,
                        new List <ObjectId>(),
                        instructorId,
                        new List <SupportMaterial>(),
                        new List <Requirement>(),
                        tags,
                        false,
                        null,
                        new List <Subject>(),
                        dbModule.CertificateUrl,
                        tutorsIds,
                        dbModule.ExtraInstructorIds,
                        dbModule.StoreUrl,
                        dbModule.EcommerceUrl,
                        dbModule.ModuleGradeType,
                        dbModule.ValidFor
                        ).Data;

                    await _db.ModuleCollection.InsertOneAsync(
                        dbModuleClone, cancellationToken : cancellationToken
                        );

                    dbModuleQuestions = await GetModuleQuestions(request.ModuleId);

                    var draft = ModuleDraft.Create(
                        dbModuleClone.Id, dbModuleClone.Title, dbModuleClone.Excerpt, dbModuleClone.ImageUrl, dbModuleClone.Published,
                        instructorId, dbModuleClone.Instructor, dbModuleClone.InstructorMiniBio, dbModuleClone.InstructorImageUrl,
                        null, null, null, dbModuleClone.CertificateUrl, dbModuleClone.StoreUrl, dbModuleClone.EcommerceUrl, request.CreateInEcommerce ?? false,
                        tags, tutorsIds
                        ).Data;

                    draft.Subjects         = dbModule.Subjects;
                    draft.Requirements     = dbModule.Requirements;
                    draft.SupportMaterials = dbModule.SupportMaterials;

                    await _db.ModuleDraftCollection.InsertOneAsync(
                        draft, cancellationToken : cancellationToken
                        );

                    if (dbModuleQuestions.Count > 0)
                    {
                        var draftQuestions = dbModuleQuestions.Select(q =>
                                                                      QuestionDraft.Create(
                                                                          ObjectId.GenerateNewId(), draft.Id,
                                                                          q.Text, q.Level, q.Duration,
                                                                          q.Concepts, q.Answers,
                                                                          dbModuleClone.Id, q.SubjectId
                                                                          ).Data
                                                                      );

                        await _db.QuestionDraftCollection.InsertManyAsync(
                            draftQuestions, cancellationToken : cancellationToken
                            );
                    }


                    return(Result.Ok(draft));
                }
                catch (Exception ex)
                {
                    return(Result.Fail <ModuleDraft>(ex.Message));
                }
            }
Ejemplo n.º 2
0
            private async Task<ModuleDraft> CreateModuleDraft(ObjectId userId, Contract request, Module originalModule,
                Duration duration, Duration videoDuration, CancellationToken token
            ) {
                var tags = new List<Tag>();
                foreach (var tagStr in request.Tags)
                {
                    var tag = Tag.Create(tagStr);
                    tags.Add(tag.Data);
                }

                var tutorsIds = request.TutorsIds != null ?
                    request.TutorsIds.Select(x => ObjectId.Parse(x)).ToList() :
                    new List<ObjectId>();

                var instructorId = String.IsNullOrEmpty(request.InstructorId) ?
                    ObjectId.Empty : ObjectId.Parse(request.InstructorId);

                var extraInstructorIds = request.ExtraInstructorIds != null ?
                    request.ExtraInstructorIds.Select(x => ObjectId.Parse(x)).ToList() :
                    new List<ObjectId>();

                var moduleId = ObjectId.Parse(request.Id);

                var module = ModuleDraft.Create(
                    moduleId, request.Title, request.Excerpt, request.ImageUrl, request.Published,
                    instructorId, request.Instructor, request.InstructorMiniBio, request.InstructorImageUrl,
                    duration, videoDuration, request.VideoUrl, request.CertificateUrl, request.StoreUrl, request.EcommerceUrl,
                    request.CreateInEcommerce.HasValue ? request.CreateInEcommerce.Value : false,
                    tags, tutorsIds, extraInstructorIds, request.ModuleGradeType
                ).Data;

                module.Subjects = originalModule.Subjects;
                module.Requirements = originalModule.Requirements;
                module.SupportMaterials = originalModule.SupportMaterials;

                await _db.ModuleDraftCollection.InsertOneAsync(
                    module, cancellationToken: token
                );

                var creationLog = AuditLog.Create(userId, moduleId, module.GetType().ToString(),
                    JsonConvert.SerializeObject(module), EntityAction.Add);

                await _db.AuditLogCollection.InsertOneAsync(creationLog);

                var dbModuleQuestions = await _db.QuestionCollection.AsQueryable()
                    .Where(q => q.ModuleId == moduleId)
                    .ToListAsync();

                if (dbModuleQuestions.Count > 0)
                {
                    var draftQuestions = dbModuleQuestions.Select(q =>
                        QuestionDraft.Create(
                            q.Id, module.Id,
                            q.Text, q.Level, q.Duration,
                            q.Concepts, q.Answers,
                            q.ModuleId, q.SubjectId
                        ).Data
                    );

                    await _db.QuestionDraftCollection.InsertManyAsync(
                        draftQuestions, cancellationToken: token
                    );
                }

                return module;
            }
Ejemplo n.º 3
0
            public async Task<Result> Handle(Contract request, CancellationToken cancellationToken)
            {
                if (request.UserRole != "Admin" && request.UserRole != "Author")
                    return Result.Fail("Acesso Negado");

                var userId = ObjectId.Parse(request.UserId);
                var modId = ObjectId.Parse(request.Id);

                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 (request.UserRole == "Author" && module.CreatedBy != userId)
                    return Result.Fail("Você não tem permissão para alterar o módulo selecionado.");

                Duration duration = null;
                if (request.Duration.HasValue)
                {
                    var durationResult = Duration.Create(request.Duration.Value);
                    if (durationResult.IsFailure)
                        return Result.Fail(durationResult.Error);

                    duration = durationResult.Data;
                }

                Duration videoDuration = null;
                if (request.VideoDuration.HasValue)
                {
                    var durationResult = Duration.Create(request.VideoDuration.Value);
                    if (durationResult.IsFailure)
                        return Result.Fail(durationResult.Error);

                    videoDuration = durationResult.Data;
                }

                if (request.TutorsIds == null)
                    request.TutorsIds = new List<string>();

                if (request.ExtraInstructorIds == null)
                    request.ExtraInstructorIds = new List<string>();

                if (module == null)
                {
                    var originalModule = await GetModule(request.Id);
                    if (originalModule == null)
                        return Result.Fail("Módulo não existe");

                    module = await CreateModuleDraft(userId, request, originalModule, 
                        duration, videoDuration, cancellationToken
                    );
                }
                else
                {
                    var oldValues = JsonConvert.SerializeObject(new List<ModuleDraft>
                    {
                        module
                    });

                    module.Title = request.Title;
                    module.Excerpt = request.Excerpt;
                    module.InstructorId = string.IsNullOrEmpty(request.InstructorId) ? ObjectId.Empty : ObjectId.Parse(request.InstructorId);
                    module.Instructor = request.Instructor;
                    module.InstructorMiniBio = request.InstructorMiniBio;
                    module.InstructorImageUrl = request.InstructorImageUrl;
                    module.ImageUrl = request.ImageUrl;
                    module.VideoUrl = request.VideoUrl;
                    module.VideoDuration = videoDuration;
                    module.Published = request.Published;
                    module.Duration = duration;
                    module.Tags = request.Tags.Select(
                        t => Tag.Create(t).Data
                    ).ToList();
                    module.CertificateUrl = request.CertificateUrl;
                    module.TutorsIds = request.TutorsIds.Select(x => ObjectId.Parse(x)).ToList();
                    module.ExtraInstructorIds = request.ExtraInstructorIds.Select(x => ObjectId.Parse(x)).ToList();
                    module.StoreUrl = request.StoreUrl;
                    module.EcommerceUrl = request.EcommerceUrl;
                    module.CreateInEcommerce = request.CreateInEcommerce.HasValue ? request.CreateInEcommerce.Value : false;
                    module.EcommerceId = request.EcommerceId;
                    module.ModuleGradeType = request.ModuleGradeType;
                    module.ValidFor = request.ValidFor;

                    await _db.ModuleDraftCollection.ReplaceOneAsync(t =>
                        t.Id == module.Id, module,
                        cancellationToken: cancellationToken
                    );

                    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);

                }

                return Result.Ok();
            }
Ejemplo n.º 4
0
            public async Task <Result <ModuleDraft> > Handle(Contract request, CancellationToken cancellationToken)
            {
                if (request.UserRole != "Admin" && request.UserRole != "Author")
                {
                    return(Result.Fail <ModuleDraft>("Acesso Negado"));
                }

                var userId = ObjectId.Parse(request.UserId);
                var tags   = new List <Tag>();

                foreach (var tagStr in request.Tags)
                {
                    var tag = Tag.Create(tagStr);
                    tags.Add(tag.Data);
                }

                var tutorsIds = request.TutorsIds != null?
                                request.TutorsIds.Select(x => ObjectId.Parse(x)).ToList() :
                                    new List <ObjectId>();

                var instructorId = String.IsNullOrEmpty(request.InstructorId) ?
                                   ObjectId.Empty : ObjectId.Parse(request.InstructorId);

                var extraInstructorIds = request.ExtraInstructorIds != null?
                                         request.ExtraInstructorIds.Select(x => ObjectId.Parse(x)).ToList() :
                                             new List <ObjectId>();

                Module          dbModule          = null;
                List <Question> dbModuleQuestions = new List <Question>();

                if (!String.IsNullOrEmpty(request.ModuleId))
                {
                    dbModule = await GetModule(request.ModuleId);
                }

                if (dbModule == null)
                {
                    dbModule = Module.Create(
                        request.Title,
                        request.Excerpt,
                        request.Instructor,
                        request.ImageUrl,
                        new List <ObjectId>(),
                        instructorId,
                        new List <SupportMaterial>(),
                        new List <Requirement>(),
                        tags,
                        request.Published ?? false,
                        null,
                        new List <Subject>(),
                        request.CertificateUrl,
                        tutorsIds,
                        extraInstructorIds,
                        request.StoreUrl,
                        request.EcommerceUrl,
                        request.ModuleGradeType,
                        request.ValidFor
                        ).Data;

                    await _db.ModuleCollection.InsertOneAsync(
                        dbModule, cancellationToken : cancellationToken
                        );
                }
                else
                {
                    dbModuleQuestions = await GetModuleQuestions(request.ModuleId);
                }

                var draft = ModuleDraft.Create(
                    dbModule.Id, request.Title, request.Excerpt, request.ImageUrl, request.Published ?? false,
                    instructorId, request.Instructor, request.InstructorMiniBio, request.InstructorImageUrl,
                    null, null, null, request.CertificateUrl, request.StoreUrl, request.EcommerceUrl, request.CreateInEcommerce ?? false,
                    tags, tutorsIds, extraInstructorIds, request.ModuleGradeType
                    ).Data;

                var newDraftList = new List <ModuleDraft>
                {
                    draft
                };

                await _db.ModuleDraftCollection.InsertOneAsync(
                    draft, cancellationToken : cancellationToken
                    );

                var creationLog = AuditLog.Create(userId, draft.Id, draft.GetType().ToString(),
                                                  JsonConvert.SerializeObject(newDraftList), EntityAction.Add);

                await _db.AuditLogCollection.InsertOneAsync(creationLog);

                if (dbModuleQuestions.Count > 0)
                {
                    var draftQuestions = dbModuleQuestions.Select(q =>
                                                                  QuestionDraft.Create(
                                                                      q.Id, draft.Id,
                                                                      q.Text, q.Level, q.Duration,
                                                                      q.Concepts, q.Answers,
                                                                      q.ModuleId, q.SubjectId
                                                                      ).Data
                                                                  );

                    await _db.QuestionDraftCollection.InsertManyAsync(
                        draftQuestions, cancellationToken : cancellationToken
                        );
                }

                return(Result.Ok(draft));
            }