Exemplo n.º 1
0
        public IActionResult Submit(int id)
        {
            var homework = _context.Homeworks
                           .Include(h => h.Subject)
                           .FirstOrDefault(h => h.Id == id);

            if (homework == null)
            {
                return(NotFound());
            }

            var model = new HomeworkSubmitViewModel
            {
                Id          = homework.Id,
                SubjectName = homework.Subject.Name,
                Name        = homework.Name
            };

            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Submit([Bind("Id,Document")] HomeworkSubmitViewModel submitViewModel)
        {
            var homework = _context.Homeworks
                           .Include(h => h.Subject)
                           .FirstOrDefault(h => h.Id == submitViewModel.Id);

            if (homework == null)
            {
                return(NotFound());
            }

            if (homework.Deadline < DateTime.Now)
            {
                ModelState.AddModelError("Document", "Deadline passed");
            }
            if (!ModelState.IsValid)
            {
                submitViewModel.Name        = homework.Name;
                submitViewModel.SubjectName = homework.Subject.Name;

                return(View(submitViewModel));
            }

            var submited = await _context.SubmitedHomeworks
                           .Include(sh => sh.Document)
                           .FirstOrDefaultAsync(sh => sh.StudentId == User.GetLoggedInUserId <string>() && sh.HomeworkId == homework.Id);

            if (submited != null)
            {
                _context.SubmitedHomeworks.Remove(submited);
                _context.Documents.Remove(submited.Document);
            }

            var extension = Path.GetExtension(submitViewModel.Document.FileName);

            if (extension == null || extension.Length > 4)
            {
                return(BadRequest("Not supported file type"));
            }
            var document = new Document
            {
                Name        = $"homework_{homework.Id}_{Guid.NewGuid()}",
                ContentType = submitViewModel.Document.ContentType,
                Extension   = extension
            };

            using (var memoryStream = new MemoryStream())
            {
                await submitViewModel.Document.CopyToAsync(memoryStream);

                if (memoryStream.Length > 2097152)
                {
                    ModelState.AddModelError("File", "The file is larger than 2MB.");
                    submitViewModel.Name        = homework.Name;
                    submitViewModel.SubjectName = homework.Subject.Name;

                    return(View(submitViewModel));
                }

                document.Data = memoryStream.ToArray();
            }

            _context.Documents.Add(document);
            submited = new SubmitedHomework
            {
                StudentId  = User.GetLoggedInUserId <string>(),
                HomeworkId = homework.Id,
                Time       = DateTime.Now,
                Document   = document
            };
            _context.SubmitedHomeworks.Add(submited);

            _context.SaveChanges();
            return(RedirectToAction(nameof(Details), new { id = homework.Id }));
        }