public ActionResult Create(Submission submission) { if (_dataLimiteSubmissaoExcedido) { TempData["message"] = "Data limite de submissão excedido"; return RedirectToAction("Index"); } const int LIMITE_SUBMISSOES = 10; var userID = ((CustomPrincipal)HttpContext.User).UserID; if (_db.Submissions.Count(t => t.UserID == userID) >= LIMITE_SUBMISSOES) { ViewBag.Message = "Limite de 10 submissões atingido"; return RedirectToAction("Index"); } submission.UserID = userID; submission.CreatedOn = DateTime.Now; submission.ModifiedOn = DateTime.Now; if (ModelState.IsValid) { _db.Submissions.Add(submission); _db.SaveChanges(); return Request.Files.Count == 0 ? RedirectToAction("Index") : UploadFile(submission); } return View(submission); }
private ActionResult UploadFile(Submission submission) { if (Request.Files.Count == 0) return RedirectToAction("Index"); var postedFile = Request.Files[0]; if (postedFile == null) return RedirectToAction("Index"); if (postedFile.FileName.Length < 4) return RedirectToAction("Index"); var extension = postedFile.FileName.Substring(postedFile.FileName.Length - 4, 4); if (!extension.Equals(".zip")) TempData["Message"] = "Formato do arquivo diferente de .zip"; else if (postedFile.ContentLength / 1024 > 10240) TempData["Message"] = "Tamanho máximo permitido: 10Mb"; else { var fileName = string.Concat(Guid.NewGuid(), ".zip"); var filePath = string.Concat(Server.MapPath("~/Uploads/"), fileName); postedFile.SaveAs(filePath); submission.FilePath = fileName; submission.ModifiedOn = DateTime.Now; using (var partialdb = new DevdayEntities()) { partialdb.Submissions.Attach(submission); partialdb.Entry(submission).Property(e => e.FilePath).IsModified = true; partialdb.Entry(submission).Property(e => e.ModifiedOn).IsModified = true; partialdb.SaveChanges(); } } return RedirectToAction("Index"); }
public ActionResult Edit(Submission submission) { if (_dataLimiteSubmissaoExcedido) { TempData["message"] = "Data limite de submissão excedido"; return RedirectToAction("Index"); } var userID = ((CustomPrincipal)HttpContext.User).UserID; var submissionCheck = _db.Submissions.FirstOrDefault(t => t.UserID == userID && t.ID == submission.ID); if (submissionCheck == null) return RedirectToAction("Index"); submission.UserID = userID; submission.ModifiedOn = DateTime.Now; if (ModelState.IsValid) { using (var partialdb = new DevdayEntities()) { partialdb.Submissions.Attach(submission); partialdb.Entry(submission).Property(e => e.Name).IsModified = true; partialdb.Entry(submission).Property(e => e.Description).IsModified = true; partialdb.Entry(submission).Property(e => e.ModifiedOn).IsModified = true; partialdb.SaveChanges(); } return Request.Files.Count == 0 ? RedirectToAction("Index") : UploadFile(submission); } return View(submission); }