Beispiel #1
0
        /**
         * Método para criar uma proposta de tfc
         */
        public async Task <IActionResult> Create(int id)
        {
            var tfc           = _context.Tfc.FromSqlRaw("Select * from Tfc where TfcId = " + id).ToList().FirstOrDefault();
            var studentNumber = int.Parse(User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value.ToString());
            var workplanList  = _context.WorkPlan
                                .FromSqlRaw("Select * from WorkPlan as wp join Student as s on wp.PlanId = s.PlanIdFk where s.StudentNumber = " + studentNumber)
                                .ToList();


            if (workplanList.Count == 0)
            {
                Alert("Ocorreu um erro!", "Tem de escolher um tipo de TFC primeiro!", NotificationType.error);
                return(RedirectToAction("Index", "Tfcs"));
            }

            WorkPlan workplan = workplanList.FirstOrDefault();

            if (tfc == null || studentNumber == 0 || workplan == null || workplan != null && !workplan.TfcType.Equals(tfc.TfcType))
            {
                Alert("Ocorreu um erro!", "Tem que escolher um tipo de TFC primeiro!", NotificationType.error);
                return(RedirectToAction("Index", "Tfcs"));
            }

            string folderName  = "StudentPlanFiles";
            string webRootPath = _hostingEnvironment.WebRootPath;
            string newPath     = Path.Combine(webRootPath, folderName);

            if (workplan.PlanFile != null)
            {
                // Delete existing file paths of the student from Directory
                System.IO.DirectoryInfo di = new DirectoryInfo(newPath);
                foreach (FileInfo filesDelete in di.GetFiles())
                {
                    var name      = filesDelete.FullName;
                    var split     = name.Split("\\");
                    var finalPath = "wwwroot/StudentPlanFiles/" + split[split.Length - 1];

                    if (finalPath.Equals(workplan.PlanFile))
                    {
                        filesDelete.Delete();
                    }
                }// End Deleting files from directories
            }

            try
            {
                if (workplan == null)
                {
                    workplan         = new WorkPlan();
                    workplan.TfcType = tfc.TfcType;
                    _context.Add(workplan);
                    var student = _context.WorkPlan.FromSqlRaw("Select * from Student where StudentNumber = " + studentNumber).ToList().FirstOrDefault();
                    student.PlanId = workplan.PlanId;
                }

                workplan.Confirmed = 0;
                workplan.PlanFile  = null;
                workplan.TfcIdFk   = null;

                var oldTfcProposal = _context.TfcProposal.FromSqlRaw("Select * from TfcProposal where StudentNumber = " + studentNumber).ToList().FirstOrDefault();

                if (oldTfcProposal == null)
                {
                    TfcProposal tfcProposal = new TfcProposal();
                    tfcProposal.StudentNumber = studentNumber;
                    tfcProposal.TfcId         = id;
                    _context.Add(tfcProposal);

                    foreach (var ruc in getRUCs())
                    {
                        Notification notification = new Notification {
                            Message = "Recebeu um novo pedido para escolha de TFC do aluno " + studentNumber + ". Vá ao paínel de propostas de TFC, para aceitar/rejeitar o pedido", state = "fechado", AddedOn = DateTime.Now, UserId = ruc.UserId, ReadNotification = 0
                        };

                        _context.Add(notification);
                    }

                    _context.SaveChanges();
                }
                else
                {
                    oldTfcProposal.TfcId = id;

                    foreach (var ruc in getRUCs())
                    {
                        Notification notification = new Notification {
                            Message = "A proposta para escolha de TFC do aluno " + studentNumber + " foi alterada. Vá ao paínel de propostas de TFC, para aceitar/rejeitar o pedido", state = "fechado", AddedOn = DateTime.Now, UserId = ruc.UserId, ReadNotification = 0
                        };

                        _context.Add(notification);
                    }

                    _context.SaveChanges();
                }
                Alert("Proposata enviada!", "A proposta para " + tfc.TfcType + " foi enviada ao responsável da UC!", NotificationType.success);
            }
            catch
            {
                Alert("Ocorreu um erro!", "Não foi possivel enviar a proposta!", NotificationType.error);
            }

            return(RedirectToAction("Index", "Tfcs"));
        }
Beispiel #2
0
        public async Task <IActionResult> CreateFromNew(String TfcType, IFormFile fil)
        {
            var      studentNumber = int.Parse(User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value.ToString());
            WorkPlan workplan      = _context.WorkPlan
                                     .FromSqlRaw("Select * from WorkPlan as wp join Student as s on wp.PlanId = s.PlanIdFk where s.StudentNumber = " + studentNumber)
                                     .ToList().FirstOrDefault();

            if (studentNumber == 0 || workplan != null && !workplan.TfcType.Equals(TfcType) || fil == null)
            {
                return(NotFound());
            }

            string folderName  = "StudentPlanFiles";
            string webRootPath = _hostingEnvironment.WebRootPath;
            string newPath     = Path.Combine(webRootPath, folderName);

            if (!Directory.Exists(newPath))// Create New Directory if not exist as per the path
            {
                Directory.CreateDirectory(newPath);
            }
            var fiName = Guid.NewGuid().ToString() + Path.GetExtension(fil.FileName);

            using (var fileStream = new FileStream(Path.Combine(newPath, fiName), FileMode.Create))
            {
                fil.CopyTo(fileStream);
            }
            // Get uploaded file path with root
            string   fileName = @"wwwroot/StudentPlanFiles/" + fiName;
            FileInfo file     = new FileInfo(fileName);

            Console.WriteLine("Plan Path" + workplan.PlanFile);

            if (workplan.PlanFile != null)
            {
                // Delete existing file paths of the student from Directory
                System.IO.DirectoryInfo di = new DirectoryInfo(newPath);
                foreach (FileInfo filesDelete in di.GetFiles())
                {
                    var name      = filesDelete.FullName;
                    var split     = name.Split("\\");
                    var finalPath = "wwwroot/StudentPlanFiles/" + split[split.Length - 1];

                    if (finalPath.Equals(workplan.PlanFile))
                    {
                        filesDelete.Delete();
                    }
                }// End Deleting files from directories
            }

            try
            {
                if (workplan == null)
                {
                    workplan         = new WorkPlan();
                    workplan.TfcType = TfcType;
                    _context.Add(workplan);
                    var student = _context.WorkPlan.FromSqlRaw("Select * from Student where StudentNumber = " + studentNumber).ToList().FirstOrDefault();
                    student.PlanId = workplan.PlanId;
                }

                workplan.Confirmed = 0;
                workplan.PlanFile  = file.ToString();
                workplan.TfcIdFk   = null;
                _context.SaveChanges();

                var oldTfcProposal = _context.TfcProposal.FromSqlRaw("Select * from TfcProposal where StudentNumber = " + studentNumber).ToList().FirstOrDefault();
                if (oldTfcProposal == null)
                {
                    TfcProposal tfcProposal = new TfcProposal();
                    tfcProposal.StudentNumber = studentNumber;
                    _context.Add(tfcProposal);

                    foreach (var ruc in getRUCs())
                    {
                        Notification notification = new Notification {
                            Message = "Recebeu um novo pedido para escolha de TFC do aluno " + studentNumber + ". Vá ao paínel de propostas de TFC, para aceitar/rejeitar o pedido", state = "fechado", AddedOn = DateTime.Now, UserId = ruc.UserId, ReadNotification = 0
                        };

                        _context.Add(notification);
                    }

                    _context.SaveChanges();
                }
                else
                {
                    oldTfcProposal.TfcId = 0;
                    foreach (var ruc in getRUCs())
                    {
                        Notification notification = new Notification {
                            Message = "A proposta para escolha de TFC do aluno " + studentNumber + " foi alterada. Vá ao paínel de propostas de TFC, para aceitar/rejeitar o pedido", state = "fechado", AddedOn = DateTime.Now, UserId = ruc.UserId, ReadNotification = 0
                        };

                        _context.Add(notification);
                    }

                    _context.SaveChanges();
                }
                Alert("Proposta Enviada!", "A sua proposta para um tfc externo foi enviada com sucesso!", NotificationType.success);
            }
            catch
            {
                Alert("Ocorreu um erro!", "Não foi possivel enviar a tua proposta para tfc! Por favor tenta mais tarde!", NotificationType.error);
            }

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