Beispiel #1
0
 public void SaveOrUpdatePaidDocument(PaidDocument paidDocument)
 {
     using (var dbContext = new DataAccess.TeachersAssistantDbContext())
     {
         _unitOfWork.InitializeDbContext(dbContext);
         if (paidDocument.PaidDocumentId != null && paidDocument.PaidDocumentId > 0)
         {
             //update
             var result = _unitOfWork._paidDocumentRepository.GetById((int)paidDocument.PaidDocumentId);
             if (result != null)
             {
                 result.RoleName  = paidDocument.RoleName;
                 result.FilePath  = paidDocument.FilePath;
                 result.SubjectId = paidDocument.SubjectId;
                 _unitOfWork.SaveChanges();
             }
         }
         else
         {
             //create
             _unitOfWork._paidDocumentRepository.Add(paidDocument);
             _unitOfWork.SaveChanges();
         }
     }
 }
        public ActionResult UploadMedia(UploadMediaViewModel mediaModel)
        {
            try
            {
                GetUIDropdownLists();

                ViewBag.RoleList = GetRolesSelectList();


                var mediaType   = mediaModel.MediaType;
                var virtualPath = string.Empty;

                //Save file to relevant fileSystem:
                switch (mediaModel.RoleName.ToLower())
                {
                case "grammar11plus":
                    if (mediaType.ToLower().StartsWith("paid"))
                    {
                        virtualPath = "~/Documents/Grammar11Plus/PaidDocuments";
                    }
                    else
                    {
                        virtualPath = "~/Documents/Grammar11Plus/FreeDocuments";
                    }
                    break;

                case "stateprimary":
                    if (mediaType.ToLower().StartsWith("paid"))
                    {
                        virtualPath = "~/Documents/StatePrimary/PaidDocuments";
                    }
                    else
                    {
                        virtualPath = "~/Documents/StatePrimary/FreeDocuments";
                    }
                    break;

                case "statejunior":
                    if (mediaType.ToLower().StartsWith("paid"))
                    {
                        virtualPath = "~/Documents/StateJunior/PaidDocuments";
                    }
                    else
                    {
                        virtualPath = "~/Documents/StateJunior/FreeDocuments";
                    }
                    break;

                default:
                    if (mediaType.ToLower().StartsWith("paid"))
                    {
                        virtualPath = "~/Documents/Administration/PaidDouments";
                    }
                    else
                    {
                        virtualPath = "~/Documents/Administration/FreeDouments";
                    }
                    break;
                }


                if (mediaModel.Select != null)
                {
                    ModelState.Clear();
                    if (mediaModel.MediaId == null || string.IsNullOrEmpty(mediaType))
                    {
                        ModelState.AddModelError("MediaId", "Media Id Required");
                        return(UploadMedia(mediaModel));
                    }
                    switch (mediaType.ToLower())
                    {
                    case "paiddocument":
                        var paidDoc = _repositoryServices.GetPaidDocumentById(mediaModel.MediaId);
                        return(View(new UploadMediaViewModel {
                            MediaId = paidDoc.PaidDocumentId, RoleName = paidDoc.RoleName, Name = paidDoc.FilePath
                        }));

                    case "freedocument":
                        var freeDoc = _repositoryServices.GetFreeDocumentById(mediaModel.MediaId);
                        return(View(new UploadMediaViewModel {
                            MediaId = freeDoc.FreeDocumentId, RoleName = freeDoc.RoleName, Name = freeDoc.FilePath
                        }));

                    case "paidvideo":
                        var paidVideo = _repositoryServices.GetPaidVideoById(mediaModel.MediaId);
                        return(View(new UploadMediaViewModel {
                            MediaId = paidVideo.PaidVideoId, RoleName = paidVideo.RoleName, Name = paidVideo.FilePath
                        }));

                    case "freevideo":
                        var freeVideo = _repositoryServices.GetFreeVideoById(mediaModel.MediaId);
                        return(View(new UploadMediaViewModel {
                            MediaId = freeVideo.FreeVideoId, RoleName = freeVideo.RoleName, Name = freeVideo.FilePath
                        }));
                    }
                    return(View(mediaModel));
                }
                else if (mediaModel.Delete != null)
                {
                    ModelState.Clear();
                    if (mediaModel.MediaId == null || string.IsNullOrEmpty(mediaType))
                    {
                        ModelState.AddModelError("MediaId", "Media Id Required");
                        return(UploadMedia(mediaModel));
                    }


                    switch (mediaType.ToLower())
                    {
                    case "paiddocument":
                        dynamic doc = _repositoryServices.GetPaidDocumentById(mediaModel.MediaId);
                        if (doc != null)
                        {
                            var delFileInfo = new FileInfo(Server.MapPath(doc.FilePath));
                            if (delFileInfo.Exists)
                            {
                                delFileInfo.Delete();
                            }
                        }
                        _repositoryServices.DeletePaidDocumentById(mediaModel.MediaId);
                        return(View("SuccessfullCreation"));

                    case "freedocument":
                        doc = _repositoryServices.GetFreeDocumentById(mediaModel.MediaId);
                        if (doc != null)
                        {
                            var delFileInfo = new FileInfo(Server.MapPath(doc.FilePath));
                            if (delFileInfo.Exists)
                            {
                                delFileInfo.Delete();
                            }
                        }
                        _repositoryServices.DeleteFreeDocumentById(mediaModel.MediaId);
                        return(View("SuccessfullCreation"));

                    case "paidvideo":
                        doc = _repositoryServices.GetPaidVideoById(mediaModel.MediaId);
                        if (doc != null)
                        {
                            var delFileInfo = new FileInfo(Server.MapPath(doc.FilePath));
                            if (delFileInfo.Exists)
                            {
                                delFileInfo.Delete();
                            }
                        }
                        _repositoryServices.DeletePaidVideoById(mediaModel.MediaId);
                        return(View("SuccessfullCreation"));

                    case "freevideo":
                        doc = _repositoryServices.GetFreeVideoById(mediaModel.MediaId);
                        if (doc != null)
                        {
                            var delFileInfo = new FileInfo(Server.MapPath(doc.FilePath));
                            if (delFileInfo.Exists)
                            {
                                delFileInfo.Delete();
                            }
                        }
                        _repositoryServices.DeleteFreeVideoById(mediaModel.MediaId);
                        return(View("SuccessfullCreation"));
                    }
                    return(View(mediaModel));
                }
                else
                {
                    if (ModelState.IsValid)
                    {
                        HttpPostedFileBase file = mediaModel.MediaContent;
                        var fileName            = file.FileName;
                        var fileBuffer          = new byte[file.ContentLength];
                        var fileContent         = file.InputStream.Read(fileBuffer, 0, fileBuffer.Length);
                        file.InputStream.Flush();
                        file.InputStream.Close();

                        var physicalPath = Server.MapPath(virtualPath);
                        var dirInfo      = new DirectoryInfo(physicalPath);
                        if (!dirInfo.Exists)
                        {
                            dirInfo.Create();
                        }

                        FileInfo fileInfo = new FileInfo(physicalPath + "\\" + file.FileName);
                        if (fileInfo.Exists)
                        {
                            fileInfo.Delete();
                        }
                        using (var fileStream = fileInfo.Create())
                        {
                            fileStream.Write(fileBuffer, 0, fileBuffer.Length);
                            fileStream.Flush();
                            fileStream.Close();
                        }

                        var paidDocument = new PaidDocument
                        {
                            FilePath  = "~" + Url.Content(virtualPath) + "/" + file.FileName,
                            SubjectId = mediaModel.SubjectId,
                            RoleName  = mediaModel.RoleName
                        };
                        //Save file Path To DB:
                        switch (mediaModel.MediaType.ToLower())
                        {
                        case "paiddocument":
                            _repositoryServices.SaveOrUpdatePaidDocument(paidDocument);
                            break;

                        case "freedocument":
                            var freeDocument = AutoMapper.Mapper.Map(paidDocument, typeof(PaidDocument),
                                                                     typeof(FreeDocument));
                            _repositoryServices.SaveOrUpdateFeeDocument(freeDocument as FreeDocument);
                            break;

                        case "paidvideo":
                            var paidVideo = AutoMapper.Mapper.Map(paidDocument, typeof(PaidDocument),
                                                                  typeof(PaidVideo));
                            _repositoryServices.SaveOrUpdatePaidVideo(paidVideo as PaidVideo);
                            break;

                        case "freevideo":
                            var freeVideo = AutoMapper.Mapper.Map(paidDocument, typeof(PaidDocument),
                                                                  typeof(FreeVideo));
                            _repositoryServices.SaveOrUpdateFreeVideo(freeVideo as FreeVideo);
                            break;
                        }
                        return(View("SuccessfullCreation"));
                    }

                    return(View("UploadMedia", mediaModel));
                }
            }
            catch (Exception e)
            {
                ModelState.Clear();
                ModelState.AddModelError("FileAccess", string.Format("{0}", e.Message));
                return(View("UploadMedia"));
            }
        }