public ActionResult Lecturer(int id) { ViewBag.MainPageKey = "lecturers"; var lecturer = lecturersRepository.GetById(id); if (lecturer == null) throw new HttpException(404, "Преподаватель не найден"); var categoryName = _currentContext.LecturerCategorySet.FirstOrDefault(cat => cat.Id == lecturer.Profile.LecturerCategoryId) == null ? String.Empty : _currentContext.LecturerCategorySet.FirstOrDefault(cat => cat.Id == lecturer.Profile.LecturerCategoryId).Name; var viewModel = new LecturerViewModel() { Id = lecturer.Id, Name = lecturer.Name, Description = lecturer.Description, LecturerCategoryName = categoryName, Degree = lecturer.Profile.Degree, Email = lecturer.Profile.Email, Post = lecturer.Profile.Post, RecordOfService = lecturer.Profile.RecordOfService, Room = lecturer.Profile.Room, LecturerPhotoPath = lecturer.Photo.FileName }; return View(viewModel); }
public ActionResult EditLecturer(LecturerViewModel lecturer, HttpPostedFileBase file) { if (ModelState.IsValid) { using (new UnitOfWork(_currentContext)) { var existing = lecturersRepository.GetById(lecturer.Id); File photo = null; //save photo if (file != null) { //delete original file first (if exists) if (existing.Photo != null) { FileUploader.DeleteFile(Server.MapPath("~/" + existing.Photo.FileName)); filesRepository.Remove(existing.Photo.Id); } var ticks = DateTime.Now.Ticks; var fileName = String.Format("profile-{0}", ticks) + Path.GetExtension(file.FileName); var absoluteTempPath = Server.MapPath("~/Content/images/uploads/temp/"); var absolutePath = Server.MapPath("~/Content/images/uploads/lecturers/"); var fullVirtualPath = "~/Content/images/uploads/lecturers/" + fileName; //create a temp file first, then compress it FileUploader.UploadFile(file, fileName, absoluteTempPath); var encoder = new ImageEncoder(); encoder.Compress(absoluteTempPath + fileName, absolutePath + fileName, 220, 220); //after compressing deleting original file FileUploader.DeleteFile(absoluteTempPath + fileName); photo = new File() { FileName = fullVirtualPath, FilePurposeId = (int)FilePurposes.Profile }; } //additional info DateTime? birthDate; try { birthDate = DateTime.Parse(lecturer.BirthDate); } catch { birthDate = null; } existing.Name = lecturer.Name; existing.Description = lecturer.Description; existing.Account = accountRepository.GetByUsername(CurrentUserName); existing.CommitteeId = lecturer.CommitteeId; existing.Profile.BirthDate = birthDate; existing.Profile.Degree = lecturer.Degree; existing.Profile.Email = lecturer.Email; existing.Profile.Post = lecturer.Post; existing.Profile.Room = lecturer.Room; existing.Profile.LecturerCategoryId = lecturer.LecturerCategoryId; existing.Profile.RecordOfService = lecturer.RecordOfService; existing.Photo = photo; } TempData[Const.ActionResultInfo] = "Профиль преподавателя успешно изменен"; return RedirectToAction("Lecturers"); } TempData[Const.ActionErrorInfo] = "Невозможно отредактировать профиль преподавателя. Проверьте правильность введенных данных."; return View(); }
public ActionResult EditLecturer(int id) { ViewBag.Committees = _currentContext.CommitteeSet.ToList(); ViewBag.LecturerCategories = _currentContext.LecturerCategorySet.ToList(); var lecturer = lecturersRepository.GetById(id); var birthDate = lecturer.Profile.BirthDate.HasValue ? lecturer.Profile.BirthDate.Value.ToShortDateString() : String.Empty; var viewModel = new LecturerViewModel() { Id = lecturer.Id, Name = lecturer.Name, Description = lecturer.Description, BirthDate = birthDate, Email = lecturer.Profile.Email, CommitteeId = lecturer.CommitteeId, Post = lecturer.Profile.Post, LecturerCategoryId = lecturer.Profile.LecturerCategoryId, Degree = lecturer.Profile.Degree, LecturerPhotoPath = lecturer.Photo != null ? lecturer.Photo.FileName : String.Empty }; return View(viewModel); }
public ActionResult AddLecturer(LecturerViewModel lecturer, HttpPostedFileBase file) { if (ModelState.IsValid) { using (new UnitOfWork(_currentContext)) { File photo = null; //save photo if (file != null) { var ticks = DateTime.Now.Ticks; var fileName = String.Format("profile-{0}", ticks) + Path.GetExtension(file.FileName); var absoluteTempPath = Server.MapPath("~/Content/images/uploads/temp/"); var absolutePath = Server.MapPath("~/Content/images/uploads/lecturers/"); var fullVirtualPath = "~/Content/images/uploads/lecturers/" + fileName; //create a temp file first, then compress it FileUploader.UploadFile(file, fileName, absoluteTempPath); var encoder = new ImageEncoder(); encoder.Compress(absoluteTempPath + fileName, absolutePath + fileName, 220, 220); //after compressing deleting original file FileUploader.DeleteFile(absoluteTempPath + fileName); photo = new File() { FileName = fullVirtualPath, FilePurposeId = (int)FilePurposes.Profile }; } //additional info DateTime? birthDate; try { birthDate = DateTime.Parse(lecturer.BirthDate); } catch { birthDate = null; } var lecturerEntry = new Lecturer { Name = lecturer.Name, Description = lecturer.Description, Account = accountRepository.GetByUsername(CurrentUserName), CommitteeId = lecturer.CommitteeId, Profile = new LecturerProfile() { BirthDate = birthDate, Degree = lecturer.Degree, Email = lecturer.Email, Post = lecturer.Post, Room = lecturer.Room, LecturerCategoryId = lecturer.LecturerCategoryId, RecordOfService = lecturer.RecordOfService }, Photo = photo }; lecturersRepository.Add(lecturerEntry); } TempData[Const.ActionResultInfo] = "Профиль преподавателя создан успешно"; return RedirectToAction("Lecturers"); } TempData[Const.ActionErrorInfo] = "Невозможно сохранить профиль преподавателя. Проверьте правильность введенных данных."; return View(); }