Beispiel #1
0
        /// <summary>
        /// GETS the main projects list view
        /// </summary>
        /// <returns>Main projects list view</returns>
        /// <remarks></remarks>
        public IActionResult Projects()
        {
            if (!User.Identity.IsAuthenticated)
            {
                TempData["Got-Error"]     = "true";
                TempData["Login-Message"] = "É necessário iniciar sessão";

                return(RedirectToAction("Index", "Home"));
            }

            BOProjectViewModel viewModel = new BOProjectViewModel();
            var projects = _context.Project.Include(s => s.IdCompanyNavigation)
                           .Include(s => s.IdProjectTypeNavigation)
                           .Include(s => s.IdStudentNavigation)
                           .Include(s => s.IdBusinessAreaNavigation).ToList();


            viewModel.Projects      = projects;
            viewModel.Companies     = PopulateCompanies();
            viewModel.ProjectTypes  = PopulateProjectTypes();
            viewModel.Students      = PopulateStudents();
            viewModel.BusinessAreas = PopulateBusinessAreas();

            var mentors = _context.SchoolMentor.ToList();

            viewModel.Mentors = new List <MentorsCheckBox>();

            foreach (SchoolMentor mentor in mentors)
            {
                viewModel.Mentors.Add(new MentorsCheckBox {
                    SchoolMentor = mentor, Selected = false
                });
            }

            return(View(viewModel));
        }
Beispiel #2
0
        public async Task <IActionResult> Project(BOProjectViewModel model)
        {
            if (!User.Identity.IsAuthenticated)
            {
                TempData["Got-Error"]     = "true";
                TempData["Login-Message"] = "É necessário iniciar sessão";

                return(RedirectToAction("Index", "Home"));
            }

            if (ModelState.IsValid)
            {
                byte[] file  = null;
                byte[] video = null;

                List <ProjectDocument> attachments = new List <ProjectDocument>();

                if (model.Attachments != null)
                {
                    foreach (var formFile in model.Attachments)
                    {
                        if (formFile.Length > 0)
                        {
                            using (var memoryStream = new MemoryStream())
                            {
                                await formFile.CopyToAsync(memoryStream);

                                attachments.Add(new ProjectDocument
                                {
                                    Document = memoryStream.ToArray(),
                                    FileName = formFile.FileName,
                                    FileSize = formFile.Length / 1024 / 1024
                                });
                            }
                        }
                    }
                }

                using (var memoryStream = new MemoryStream())
                {
                    await model.Report.CopyToAsync(memoryStream);

                    file = memoryStream.ToArray();
                }

                if (model.Video != null)
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        await model.Video.CopyToAsync(memoryStream);

                        video = memoryStream.ToArray();
                    }
                }

                var project = new Project
                {
                    Title          = model.Project.Title,
                    Description    = model.Project.Description,
                    Report         = file,
                    ProjectDate    = model.Project.ProjectDate,
                    IsVisible      = model.Project.IsVisible,
                    IdCompany      = model.Project.IdCompany,
                    IdProjectType  = model.Project.IdProjectType,
                    IdStudent      = model.Project.IdStudent,
                    Grade          = model.Project.Grade,
                    IdBusinessArea = model.Project.IdBusinessArea,
                    Video          = video
                };

                _context.Add(project);
                await _context.SaveChangesAsync();

                foreach (var attachment in attachments)
                {
                    attachment.IdProject = project.IdProject;
                    await _context.AddAsync(attachment);
                }

                _context.SaveChanges();

                foreach (MentorsCheckBox mentor in model.Mentors)
                {
                    if (mentor.Selected)
                    {
                        var newMentor = new ProjectAdvisor
                        {
                            IdProject      = project.IdProject,
                            IdSchoolMentor = mentor.SchoolMentor.IdSchoolMentor
                        };

                        _context.Add(newMentor);
                    }
                }

                _context.SaveChanges();

                if (model.Project.IdCompany != 0)
                {
                    if (model.Project.IdCompany == null)
                    {
                        model.Project.IdCompany = 0;
                    }
                    SendCompanyEmail((long)model.Project.IdCompany, model.Project.Title, model.Project.IdStudent);
                }

                return(RedirectToAction("Projects", "BackOffice"));
            }

            return(RedirectToAction("Projects", "BackOffice"));
        }
Beispiel #3
0
        /// <summary>
        /// PUTS(UPDATES) a project in the database.
        /// </summary>
        /// <param name="model"></param>
        /// <returns>Projects Main List Backoffice view</returns>
        /// <remarks></remarks>
        public async Task <IActionResult> EditProject(BOProjectViewModel viewModel)
        {
            if (!User.Identity.IsAuthenticated)
            {
                TempData["Got-Error"]     = "true";
                TempData["Login-Message"] = "É necessário iniciar sessão";

                return(RedirectToAction("Index", "Home"));
            }

            if (viewModel.Report != null)
            {
                using (var memoryStream = new MemoryStream())
                {
                    await viewModel.Report.CopyToAsync(memoryStream);

                    viewModel.Project.Report = memoryStream.ToArray();
                }
            }

            viewModel.Project.ProjectAdvisor = _context.ProjectAdvisor.Where(pa => pa.IdProject == viewModel.Project.IdProject).ToList();

            foreach (var advisor in viewModel.Project.ProjectAdvisor)
            {
                _context.ProjectAdvisor.Remove(advisor);
                await _context.SaveChangesAsync();
            }

            foreach (var mentor in viewModel.Mentors)
            {
                if (mentor.Selected)
                {
                    var advisor = new ProjectAdvisor {
                        IdProject = viewModel.Project.IdProject, IdSchoolMentor = mentor.SchoolMentor.IdSchoolMentor
                    };
                    await _context.ProjectAdvisor.AddAsync(advisor);

                    await _context.SaveChangesAsync();
                }
            }

            viewModel.Project.ProjectAdvisor = new List <ProjectAdvisor>();

            if (viewModel.Attachments != null)
            {
                if (viewModel.Attachments.Count() > 0)
                {
                    viewModel.Project.ProjectDocument = _context.ProjectDocument.Where(pd => pd.IdProject == viewModel.Project.IdProject).ToList();

                    foreach (var document in viewModel.Project.ProjectDocument)
                    {
                        _context.ProjectDocument.Remove(document);
                        await _context.SaveChangesAsync();
                    }

                    foreach (var formFile in viewModel.Attachments)
                    {
                        if (formFile.Length > 0)
                        {
                            using (var memoryStream = new MemoryStream())
                            {
                                await formFile.CopyToAsync(memoryStream);

                                var document = new ProjectDocument
                                {
                                    IdProject = viewModel.Project.IdProject,
                                    Document  = memoryStream.ToArray(),
                                    FileName  = formFile.FileName,
                                    FileSize  = Convert.ToDouble(Convert.ToDecimal(formFile.Length) / 1024m / 1024m)
                                };

                                await _context.ProjectDocument.AddAsync(document);

                                await _context.SaveChangesAsync();
                            }
                        }
                    }


                    viewModel.Project.ProjectDocument = new List <ProjectDocument>();
                }
            }



            _context.Project.Update(viewModel.Project);
            _context.SaveChanges();

            TempData["HasAlert"]     = "true";
            TempData["AlertMessage"] = "Project edited successfully.";

            return(RedirectToAction("Projects", "BackOffice"));
        }