internal ResourceTopic populateTopicFromDB(int id) { using (ResourcesDataContext db = new ResourcesDataContext()) { ResourceTopic currentTopic = new ResourceTopic(); var dbTopic = db.bhdResourceTopics.Single((x) => x.id == id); currentTopic.topicId = dbTopic.id; currentTopic.parentId = dbTopic.parentId; currentTopic.name = dbTopic.name; currentTopic.description = dbTopic.description; currentTopic.active = dbTopic.isActive; if (currentTopic.parentId != null) { FindParent(currentTopic); currentTopic.isParent = false; } else { currentTopic.isParent = true; currentTopic.phaseId = currentTopic.topicId.Value; } return currentTopic; } }
public ResourceListPayload Get(int id) { ResourceListPayload payload = new ResourceListPayload(); payload.resourceList = new List<ResourceList>(); using (ResourcesDataContext dc = new ResourcesDataContext()) { var r = dc.sps_getResourceList(true, id + 1, 20, null, null); foreach (var item in r) { ResourceList tmpPayload = new ResourceList(); tmpPayload.ResourceName = item.name; tmpPayload.ResourceDescription = item.description; tmpPayload.ResourceLanguage = item.language; tmpPayload.ResourceTopic = item.topic; tmpPayload.ResourceUploadDate = item.uploadDate.ToShortDateString(); tmpPayload.ResourceId = item.id; tmpPayload.ResourceType = item.type; payload.count = (int)item.total; payload.resourceList.Add(tmpPayload); if (item.previewFileId.HasValue) { tmpPayload.PreviewFileId = (int)item.previewFileId; } else { tmpPayload.PreviewFileId = 0; } } } return payload; }
public static int UploadResourceData(UploadData data) { int retId = 0; using (ResourcesDataContext dc = new ResourcesDataContext()) { bhdResource r = new bhdResource(); r.name = data.ResourceName; r.description = data.ResourceDescription; r.languageId = data.ResourceLanguageId; r.typeId = data.ResourceTypeId; r.topicId = data.ResourceTopicId; r.uploadDate = DateTime.Now; dc.bhdResources.InsertOnSubmit(r); dc.SubmitChanges(); retId = r.id; bhdResourceFormat rf = new bhdResourceFormat(); rf.formatId = data.ResourceFormatId; rf.resourceId = r.id; dc.bhdResourceFormats.InsertOnSubmit(rf); dc.SubmitChanges(); //bhdResourceRating rr = new bhdResourceRating(); //rr.resourceId = r.id; //rr.rating = 1; //rr.userId = 1; // TODO: Update when the user is passed through. //dc.bhdResourceRatings.InsertOnSubmit(rr); //dc.SubmitChanges(); } return retId; }
public ResourceApprovePayload Get() { ResourceApprovePayload payload = new ResourceApprovePayload(); payload.resourceList = new List<ResourceList>(); using (ResourcesDataContext dc = new ResourcesDataContext()) { var r = (from d in dc.bhdResources join l in dc.bhdResourceLanguages on d.languageId equals l.id join top in dc.bhdResourceTopics on d.topicId equals top.id join typ in dc.bhdResourceTypes on d.typeId equals typ.id where !d.isActive && !d.approvalDate.HasValue && !d.approvalUser.HasValue select new { d.name, d.description, d.uploadDate, d.id, language = l.name , topic = top.name, type = typ.name, d.isActive }).Take(20); foreach (var item in r) { ResourceList tmpPayload = new ResourceList(); tmpPayload.ResourceName = item.name; tmpPayload.ResourceDescription = item.description; tmpPayload.ResourceLanguage = item.language; tmpPayload.ResourceTopic = item.topic; tmpPayload.ResourceUploadDate = item.uploadDate.ToShortDateString(); tmpPayload.ResourceId = item.id; tmpPayload.ResourceType = item.type; tmpPayload.isActive = item.isActive; payload.resourceList.Add(tmpPayload); } } return payload; }
public async Task<HttpResponseMessage> Post(int userId) { int returnId = 0; try { using (ResourcesDataContext dc = new ResourcesDataContext()) { Stream streamIn = await Request.Content.ReadAsStreamAsync(); StreamReader streamReader = new StreamReader(streamIn); string jsonstring = streamReader.ReadToEnd(); ResourceBundle newBundle = JsonConvert.DeserializeObject<ResourceBundle>(jsonstring); bhdResourceBundle rb = new bhdResourceBundle(); rb.name = newBundle.name; rb.description = newBundle.description; rb.userId = userId; rb.isActive = true; dc.bhdResourceBundles.InsertOnSubmit(rb); dc.SubmitChanges(); returnId = rb.bundleId; bhdResourceBundleFile rbf = new bhdResourceBundleFile(); rbf.resourceFileId = newBundle.fileId; rbf.bundleId = returnId; dc.bhdResourceBundleFiles.InsertOnSubmit(rbf); dc.SubmitChanges(); } } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.BadRequest, "Something Broke. " + ex.Message + ". 400-3"); } return Request.CreateResponse(HttpStatusCode.OK, returnId); }
public ResourceFormat Get(int id) { using (ResourcesDataContext db = new ResourcesDataContext()) { bhdFormat dbFormat = db.bhdFormats.Single(x => x.id == id); return new ResourceFormat { formatId = dbFormat.id, name = dbFormat.name, active = dbFormat.isActive }; } }
public List<ResourceFormat> Get() { using (ResourcesDataContext db = new ResourcesDataContext()) { List<ResourceFormat> formats = db.bhdFormats.Select(x => new ResourceFormat { formatId = x.id, name = x.name, active = x.isActive}).ToList(); return formats; } }
public async Task Test() { IResourcesDataContext dataContext = new ResourcesDataContext(); await dataContext.InitialDataContext(); Assert.IsNotNull(dataContext.Records); Assert.IsTrue(dataContext.Records.Count > 0); }
public static ResourceFile UploadResourceFile(FileData files, int resourceId) { ResourceFile resFileRet = new ResourceFile(); //we get the information using (ResourcesDataContext dc = new ResourcesDataContext()) { //check if we have the file type int typeid = 0; var type = (from d in dc.bhdFileTypes where d.contentType == files.fileType && d.isActive select d).FirstOrDefault(); if (type == null) { bhdFileType ft = new bhdFileType(); ft.contentType = files.fileType; string extension = files.fileName; int pos = extension.LastIndexOf('.'); ft.extension = extension.Substring(pos + 1, extension.Length - (pos + 1)); ft.isActive = true; dc.bhdFileTypes.InsertOnSubmit(ft); dc.SubmitChanges(); typeid = ft.id; } else { typeid = type.id; } bhdFile f = new bhdFile(); f.size = files.fileSize; f.name = files.fileName; f.isActive = true; f.fileTypeId = typeid; dc.bhdFiles.InsertOnSubmit(f); dc.SubmitChanges(); bhdFileData fd = new bhdFileData(); fd.fileId = f.id; fd.data = files.fileData; dc.bhdFileDatas.InsertOnSubmit(fd); dc.SubmitChanges(); bhdResourceFile rf = new bhdResourceFile(); rf.resourceId = resourceId; rf.fileId = f.id; dc.bhdResourceFiles.InsertOnSubmit(rf); dc.SubmitChanges(); ResourceFile tmpFileRes = new ResourceFile(); tmpFileRes.fileid = rf.fileId; tmpFileRes.filename = f.name; resFileRet = tmpFileRes; } return resFileRet; }
public List<ResourceTopic> Get() { List<ResourceTopic> topics = new List<ResourceTopic>(); using (ResourcesDataContext db = new ResourcesDataContext()) { foreach (bhdResourceTopic currentItem in db.bhdResourceTopics) { topics.Add(populateTopicFromDB(currentItem.id)); } } return topics; }
public List<GenDropList> Get() { List<GenDropList> tmpList = new List<GenDropList>(); using (ResourcesDataContext dc = new ResourcesDataContext()) { List<GenDropList> r = (from d in dc.bhdAuthors where d.isActive select new GenDropList() { ListId = d.id, ListValue = d.name }).ToList(); tmpList = r; } return tmpList; }
public FileResult Get(int id) { using (ResourcesDataContext db = new ResourcesDataContext()) { if (id == 0) id = 1; var fileInfo = db.bhdFiles.Where((x) => x.id == id).FirstOrDefault(); if (fileInfo != null) return new FileContentResult(fileInfo.bhdFileData.data.ToArray(), fileInfo.bhdFileType.contentType); else return null; } }
public async Task<HttpResponseMessage> Post() { using (ResourcesDataContext dc = new ResourcesDataContext()) { var r = (from d in dc.bhdFeaturedResources select d); foreach (var feature in r) { feature.isActive = false; feature.isFrontPage = false; } dc.SubmitChanges(); var result = Request.Content.ReadAsFormDataAsync(); Stream streamIn = await Request.Content.ReadAsStreamAsync(); StreamReader streamReader = new StreamReader(streamIn); string jsonstring = streamReader.ReadToEnd(); if (result != null) { List<FeaturedResourceModel> featuredModel; try { List<FeaturedResourceModel> tmp = (List<FeaturedResourceModel>)JsonConvert.DeserializeObject(jsonstring.ToString(), typeof(List<FeaturedResourceModel>)); featuredModel = tmp; } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.BadRequest, ex); } if (featuredModel != null) { foreach (FeaturedResourceModel item in featuredModel) { bhdFeaturedResource feat = new bhdFeaturedResource(); feat.resourceId = item.ResourceId; feat.portalId = item.PortalId; feat.sequence = item.Sequence; feat.startDate = item.sDate; feat.endDate = item.eDate; feat.isActive = true; dc.bhdFeaturedResources.InsertOnSubmit(feat); dc.SubmitChanges(); } } } } return Request.CreateResponse(HttpStatusCode.OK, "Success"); }
public async Task<HttpResponseMessage> Put() { HttpResponseMessage response = new HttpResponseMessage(); var result = Request.Content.ReadAsFormDataAsync(); Stream streamIn = await Request.Content.ReadAsStreamAsync(); StreamReader streamReader = new StreamReader(streamIn); string jsonstring = streamReader.ReadToEnd(); ResourceFormat currentFormat; try { currentFormat = JsonConvert.DeserializeObject<ResourceFormat>(jsonstring); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.BadRequest, ex); } try { using (ResourcesDataContext db = new ResourcesDataContext()) { if (currentFormat.formatId == 0) { bhdFormat newFormat = new bhdFormat(); newFormat.name = currentFormat.name; newFormat.isActive = currentFormat.active; db.bhdFormats.InsertOnSubmit(newFormat); db.SubmitChanges(); response = Request.CreateResponse(HttpStatusCode.OK, "New Format sucessfully added."); } else { bhdFormat editformat = db.bhdFormats.Single((x) => x.id == currentFormat.formatId); editformat.isActive = currentFormat.active.Value; editformat.name = currentFormat.name; db.SubmitChanges(); response = Request.CreateResponse(HttpStatusCode.OK, "Format sucessfully edited."); } } } catch (Exception ex) { Request.CreateResponse(HttpStatusCode.InternalServerError, ex); } return response; }
public ResourcePayload Get() { ResourcePayload payload = new ResourcePayload(); GenDropList tmpList = new GenDropList(); using (ResourcesDataContext dc = new ResourcesDataContext()) { List<GenDropList> r = (from d in dc.bhdResourceTypes where d.isActive select new GenDropList() { ListId = d.id, ListValue = d.name }).ToList(); payload.types = r; r = (from d in dc.bhdFormats where (bool)d.isActive select new GenDropList() { ListId = d.id, ListValue = d.name }).ToList(); payload.formats = r; r = (from d in dc.bhdResourceLanguages where d.isActive select new GenDropList() { ListId = d.id, ListValue = d.name }).ToList(); payload.languages = r; DataTable tmpDT = new DataTable(); tmpDT.TableName = "Topics"; DataColumn workCol = tmpDT.Columns.Add("id", typeof(Int32)); workCol.AllowDBNull = false; workCol.Unique = true; tmpDT.Columns.Add("parentid", typeof(Int32)); tmpDT.Columns.Add("name", typeof(String)); var tr = (from d in dc.bhdResourceTopics where d.isActive select new { d.id, d.parentId, d.name }).ToList(); foreach (var item in tr) { DataRow workRow = tmpDT.NewRow(); workRow["id"] = item.id; if (item.parentId == null) workRow["parentid"] = 0; else workRow["parentid"] = item.parentId; workRow["name"] = item.name; tmpDT.Rows.Add(workRow); } payload.topics = tmpDT; } return payload; }
public async Task<HttpResponseMessage> Post() { int ResourceId = 0, ratingGiven = 0, userid = 0; HttpResponseMessage response = new HttpResponseMessage(); var result = Request.Content.ReadAsFormDataAsync(); Stream streamIn = await Request.Content.ReadAsStreamAsync(); StreamReader streamReader = new StreamReader(streamIn); string jsonstring = streamReader.ReadToEnd(); Dictionary<string, int> currentRating = new Dictionary<string, int>(); try { currentRating = JsonConvert.DeserializeObject<Dictionary<string, int>>(jsonstring); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.BadRequest, ex); } if (currentRating != null) { if (currentRating["resourceid"] != null && currentRating["ratingscore"] != null && currentRating["userid"] != null) { using (ResourcesDataContext dc = new ResourcesDataContext()) { try { bhdResourceRating d = new bhdResourceRating(); d.resourceId = currentRating["resourceid"]; d.rating = currentRating["ratingscore"]; d.userId = currentRating["userid"]; dc.bhdResourceRatings.InsertOnSubmit(d); dc.SubmitChanges(); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.OK, ex); } } } } return response; }
public async Task<HttpResponseMessage> Post() { var result = Request.Content.ReadAsFormDataAsync(); Stream streamIn = await Request.Content.ReadAsStreamAsync(); StreamReader streamReader = new StreamReader(streamIn); string jsonstring = streamReader.ReadToEnd(); // serialize that shit AuthorData uploadModel; try { uploadModel = JsonConvert.DeserializeObject<AuthorData>(jsonstring); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.BadRequest, ex); } List<GenDropList> repsonse = new List<GenDropList>(); try { using (ResourcesDataContext dc = new ResourcesDataContext()) { bhdAuthor newAuth = new bhdAuthor(); newAuth.name = uploadModel.AuthorName; newAuth.surname = uploadModel.AuthorSurname; newAuth.isActive = true; dc.bhdAuthors.InsertOnSubmit(newAuth); dc.SubmitChanges(); repsonse = (from d in dc.bhdAuthors where d.isActive == true select new GenDropList() { ListId = d.id, ListValue = d.name + " " + d.surname }).ToList(); } } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.BadRequest, "Something Broke. " + ex.Message + ". 400-3"); } return Request.CreateResponse(HttpStatusCode.OK, repsonse); }
public async Task<HttpResponseMessage> Post() { var result = Request.Content.ReadAsFormDataAsync(); Stream streamIn = await Request.Content.ReadAsStreamAsync(); StreamReader streamReader = new StreamReader(streamIn); string jsonstring = streamReader.ReadToEnd(); // serialize that shit FileInfoData uploadModel; try { uploadModel = JsonConvert.DeserializeObject<FileInfoData>(jsonstring); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.BadRequest, ex); } try { using (ResourcesDataContext dc = new ResourcesDataContext()) { if (uploadModel.publisherid != 0 && uploadModel.authorid != 0) { bhdPublishInformation pubData = new bhdPublishInformation(); pubData.resourceId = uploadModel.resourceid; pubData.authorId = uploadModel.authorid; pubData.fileId = uploadModel.fileid; pubData.isActive = true; pubData.publisherId = uploadModel.publisherid; pubData.publishYear = uploadModel.publishYear; dc.bhdPublishInformations.InsertOnSubmit(pubData); dc.SubmitChanges(); } } } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.BadRequest, "Something Broke. " + ex.Message + ". 400-3"); } return Request.CreateResponse(HttpStatusCode.OK, "Sucess"); }
public ResourceFeaturedPayload Get() { ResourceFeaturedPayload payload = new ResourceFeaturedPayload(); payload.resourceList = new List<ResourceList>(); using (ResourcesDataContext dc = new ResourcesDataContext()) { var r = dc.sps_getFeaturedResources(0, true); foreach (var item in r) { ResourceList tmpPayload = new ResourceList(); tmpPayload.ResourceName = item.name; tmpPayload.ResourceDescription = item.description; tmpPayload.ResourceLanguage = item.language; tmpPayload.ResourceTopic = item.topic; tmpPayload.ResourceUploadDate = item.uploadDate.ToShortDateString(); tmpPayload.ResourceId = item.id; tmpPayload.ResourceType = item.type; payload.resourceList.Add(tmpPayload); } } return payload; }
public List<ResourceBundle> Get(int id) { List<ResourceBundle> bundles = new List<ResourceBundle>(); using (ResourcesDataContext dc = new ResourcesDataContext()) { foreach (var item in dc.sps_getResourceBundleByUserId(id).ToList()) { ResourceBundle rb = new ResourceBundle(); rb.bundleId = item.bundleId; rb.description = item.description; rb.fileExtension = item.fileExtension; rb.fileId = item.fileid; rb.fileName = item.fileName; rb.fileSize = item.fileSize; rb.fileType = item.fileType; rb.isActive = item.isActive; rb.name = item.name; bundles.Add(rb); } } return bundles; }
public List<TopicInfo> Get() { using (ResourcesDataContext db = new ResourcesDataContext()) { Dictionary<int, string> tmpParents = new Dictionary<int, string>(); List<TopicInfo> tmpList = new List<TopicInfo>(); var r = db.sps_getResourceTopic(null); foreach (var item in r) { if (item.parentId.HasValue) { string tmp; tmpParents.TryGetValue((int)item.parentId, out tmp); tmpParents.Add(item.id, tmp + " > " + item.name); TopicInfo mtp = new TopicInfo(); mtp.topicId = item.id; mtp.parentId = item.parentId; mtp.name = item.name; //if (item.linkedResources != null) // mtp.ListValue = mtp.ListValue + "(" + item.linkedResources.ToString() + ")"; tmpList.Add(mtp); } else { tmpParents.Add(item.id, item.name); TopicInfo mtp = new TopicInfo(); mtp.topicId = item.id; mtp.parentId = item.parentId; mtp.name = item.name; //if (item.linkedResources != null) // mtp.ListValue = mtp.ListValue + "(" + item.linkedResources.ToString() + ")"; tmpList.Add(mtp); } } return tmpList; } }
public List<menuItem> Get(int tabid, int moduleid) { using (ResourcesDataContext db = new ResourcesDataContext()) { if (db.bhdMenuPages.Any((x) => x.tabId == tabid && x.moduleId == moduleid)) { var m = db.bhdMenuPages.Single((x) => x.tabId == tabid && x.moduleId == moduleid); var menuList = m.bhdMenu.bhdMenuItems.ToList(); if (menuList.Any()) { List<menuItem> menuItemList = new List<menuItem>(); foreach (bhdMenuItem currentMenuItem in menuList) { int _tabid = 0; int _moduleid = 0; int.TryParse(currentMenuItem.tabId.ToString(), out _tabid); int.TryParse(currentMenuItem.moduleId.ToString(), out _moduleid); //menuItem item = new menuItem(currentMenuItem.id, currentMenuItem.text, currentMenuItem.hoverText, _tabid, _moduleid, currentMenuItem.url, currentMenuItem.isActive); menuItem item = new menuItem(); item.menuId = currentMenuItem.menuId; item.text = currentMenuItem.text; item.hoverText = currentMenuItem.hoverText; item.tabId = _tabid; item.moduleId = _moduleid; item.url = currentMenuItem.url; item.isActive = currentMenuItem.isActive; item.isAdmin = currentMenuItem.isAdmin; menuItemList.Add(item); } return menuItemList; } else return null; } else return null; } }
public ResourceList Get(int id) { using (ResourcesDataContext dc = new ResourcesDataContext()) { var r = (from d in dc.bhdResources join l in dc.bhdResourceLanguages on d.languageId equals l.id join top in dc.bhdResourceTopics on d.topicId equals top.id join typ in dc.bhdResourceTypes on d.typeId equals typ.id where !d.isActive && !d.approvalDate.HasValue && !d.approvalUser.HasValue && d.id == id select new { d.name, d.description, d.uploadDate, d.id, language = l.name, topic = top.name, type = typ.name, d.isActive }).FirstOrDefault(); ResourceList resource = new ResourceList(); resource.ResourceName = r.name; resource.ResourceDescription = r.description; resource.ResourceLanguage = r.language; resource.ResourceTopic = r.topic; resource.ResourceUploadDate = r.uploadDate.ToShortDateString(); resource.ResourceId = r.id; resource.ResourceType = r.type; resource.isActive = r.isActive; return resource; } }
public async Task<HttpResponseMessage> Put(int id) { try { Stream streamIn = await Request.Content.ReadAsStreamAsync(); StreamReader streamReader = new StreamReader(streamIn); string jsonstring = streamReader.ReadToEnd(); List<ResourceBundle> newBundles = JsonConvert.DeserializeObject<List<ResourceBundle>>(jsonstring); using (ResourcesDataContext dc = new ResourcesDataContext()) { bhdResourceBundle currentBundle = dc.bhdResourceBundles.Single((x) => x.bundleId == id); List<bhdResourceBundleFile> bundleFiles = dc.bhdResourceBundleFiles.Where((x) => x.bundleId == id).ToList(); if ((newBundles.Count() == 0 && id != 0) || (currentBundle.isActive != newBundles.First().isActive)) { currentBundle.isActive = false; dc.SubmitChanges(); return Request.CreateResponse(HttpStatusCode.OK, string.Format("Bundle {0} successfully removed.", currentBundle.name)); } else if (newBundles.Count() > 0 && id != 0) { foreach (ResourceBundle rb in newBundles) { if (!bundleFiles.Any((x) => x.resourceFileId == rb.fileId)) { bhdResourceBundleFile newFile = new bhdResourceBundleFile(); newFile.resourceFileId = rb.fileId; newFile.bundleId = id; newFile.isFavourite = rb.isFavourite; dc.bhdResourceBundleFiles.InsertOnSubmit(newFile); dc.SubmitChanges(); } else { bhdResourceBundleFile currentfile = bundleFiles.Single((x) => x.resourceFileId == rb.fileId); currentfile.resourceFileId = rb.fileId; currentfile.bundleId = rb.bundleId; currentfile.isFavourite = rb.isFavourite; dc.SubmitChanges(); } } bundleFiles = dc.bhdResourceBundleFiles.Where((x) => x.bundleId == id).ToList(); foreach (bhdResourceBundleFile bf in bundleFiles) { if (!newBundles.Any((x) => x.fileId == bf.resourceFileId)) { dc.bhdResourceBundleFiles.DeleteOnSubmit(bf); dc.SubmitChanges(); } } } return Request.CreateResponse(HttpStatusCode.OK, string.Format("Bundle files sucessfully for {0}", currentBundle.name)); } } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.BadRequest, "Something Broke. " + ex.Message + ". 400-3"); } }
public async Task<HttpResponseMessage> Put(int id) { var result = Request.Content.ReadAsFormDataAsync(); Stream streamIn = await Request.Content.ReadAsStreamAsync(); StreamReader streamReader = new StreamReader(streamIn); string jsonstring = streamReader.ReadToEnd(); ResourceList currentResource; try { currentResource = JsonConvert.DeserializeObject<ResourceList>(jsonstring); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.BadRequest, ex); } try { using (ResourcesDataContext db = new ResourcesDataContext()) { bhdResource approvalResource = db.bhdResources.Single((x) => x.id == currentResource.ResourceId); approvalResource.isActive = currentResource.isActive; approvalResource.approvalDate = DateTime.Now; approvalResource.approvalUser = id; db.SubmitChanges(); } if (currentResource.isActive) return Request.CreateResponse(HttpStatusCode.OK, "Resource approved!"); else return Request.CreateResponse(HttpStatusCode.OK, "Resource declined."); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.BadRequest, ex); } }
public void Delete(int id) { //this is where we deactivate resources using (ResourcesDataContext dc = new ResourcesDataContext()) { var r = (from d in dc.bhdResources join l in dc.bhdResourceLanguages on d.languageId equals l.id join top in dc.bhdResourceTopics on d.topicId equals top.id join typ in dc.bhdResourceTypes on d.typeId equals typ.id where d.id == id select d).FirstOrDefault(); if (r != null) { r.isActive = false; dc.SubmitChanges(); } } }
public async Task<HttpResponseMessage> Post(int id) { try { var result = Request.Content.ReadAsFormDataAsync(); Stream streamIn = await Request.Content.ReadAsStreamAsync(); StreamReader streamReader = new StreamReader(streamIn); string jsonstring = streamReader.ReadToEnd(); var temp = JsonConvert.DeserializeObject<TagsInfo>(jsonstring); using (ResourcesDataContext dc = new ResourcesDataContext()) { foreach (var item in temp.tags) { var r = (from d in dc.bhdKeywords where d.isActive && d.value == item select d).FirstOrDefault(); if (r != null) { bhdResourceKeyword tmpResourceKeyword = new bhdResourceKeyword(); tmpResourceKeyword.KeywordId = r.id; tmpResourceKeyword.Resourceid = temp.ResourceId; dc.bhdResourceKeywords.InsertOnSubmit(tmpResourceKeyword); dc.SubmitChanges(); } else { bhdKeyword tmpkeyword = new bhdKeyword(); tmpkeyword.value = item; tmpkeyword.isActive = true; dc.bhdKeywords.InsertOnSubmit(tmpkeyword); dc.SubmitChanges(); bhdResourceKeyword tmpResourceKeyword = new bhdResourceKeyword(); tmpResourceKeyword.KeywordId = tmpkeyword.id; tmpResourceKeyword.Resourceid = temp.ResourceId; dc.bhdResourceKeywords.InsertOnSubmit(tmpResourceKeyword); dc.SubmitChanges(); } } } return Request.CreateResponse(HttpStatusCode.OK, "Success"); } catch (Exception) { return Request.CreateResponse(HttpStatusCode.BadRequest, "Something Broke. 400-1"); } }
public async Task<HttpResponseMessage> Put() { var result = Request.Content.ReadAsFormDataAsync(); Stream streamIn = await Request.Content.ReadAsStreamAsync(); StreamReader streamReader = new StreamReader(streamIn); string jsonstring = streamReader.ReadToEnd(); // serialize that shit List<int> id; try { id = JsonConvert.DeserializeObject<List<int>>(jsonstring); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.BadRequest, ex); } if (id.Count() == 0) return Request.CreateResponse(HttpStatusCode.BadRequest, "No file id sent through."); try { using (ResourcesDataContext db = new ResourcesDataContext()) { FileDownloadData tmpDownloadData = new FileDownloadData(); if (id.Count() == 1) { bhdFileData fileData = db.bhdFileDatas.Single((x) => x.fileId == id.First()); tmpDownloadData.FileData = fileData.data.ToArray(); tmpDownloadData.ContentDisposition = "attachment"; tmpDownloadData.ContentDispositionFileName = fileData.bhdFile.name; return Request.CreateResponse(HttpStatusCode.OK, tmpDownloadData); } else { FileDownloadData tmpZipFileData = new FileDownloadData(); HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); string archiveName = String.Format("justforteachers-{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss")); MemoryStream fs = new MemoryStream(); using (ZipFile zf = new ZipFile()) { int filenum = 1; foreach (int fileId in id) { bhdFileData fileData = db.bhdFileDatas.Single((x) => x.fileId == id.First()); zf.AddEntry(filenum.ToString() + " - " + fileData.bhdFile.name, fileData.data.ToArray()); filenum++; } zf.Save(fs); } BinaryReader br = new BinaryReader(fs); tmpZipFileData.ContentDisposition = "attachement"; tmpZipFileData.ContentDispositionFileName = archiveName; tmpZipFileData.FileData = fs.ToArray(); return Request.CreateResponse(HttpStatusCode.OK, tmpZipFileData); } } } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.BadRequest, string.Format("No File(s) found. /r/n {0}", ex.Message)); } }
public ResourceEditPayload Get(int id) { //this is to get a resource ResourceEditPayload tmpResource = new ResourceEditPayload(); ResourceEditList tmpList = new ResourceEditList(); using (ResourcesDataContext dc = new ResourcesDataContext()) { var r = (from d in dc.bhdResources join l in dc.bhdResourceLanguages on d.languageId equals l.id join top in dc.bhdResourceTopics on d.topicId equals top.id join typ in dc.bhdResourceTypes on d.typeId equals typ.id where d.id == id select new { ResourceName = d.name, ResourceDescription = d.description, ResourceUpload = d.uploadDate, ResourceId = d.id, ResourceLanguageId = l.id, ResourceLanguage = l.name, ResourceTopicId = top.id, ResourceTopic = top.name, ResourceTypeId = typ.id, ResourceType = typ.name }).FirstOrDefault(); if (r != null) { tmpList.ResourceName = r.ResourceName; tmpList.ResourceDescription = r.ResourceDescription; tmpList.ResourceUploadDate = r.ResourceUpload.ToShortDateString(); tmpList.ResourceId = r.ResourceId; tmpList.ResourceLanguage = r.ResourceLanguage; tmpList.ResourceTopic = r.ResourceTopic; tmpList.ResourceType = r.ResourceType; tmpList.ResourceLanguageId = r.ResourceLanguageId; tmpList.ResourceTopicId = r.ResourceTopicId; tmpList.ResourceTypeId = r.ResourceTypeId; } tmpResource.resourceInfo = tmpList; if (r.ResourceTypeId == 1) { List<FileViewInfo> files = (from f in dc.bhdResourceFiles join a in dc.bhdPublishInformations on r.ResourceId equals a.resourceId where f.resourceId == r.ResourceId && f.fileId == a.fileId select new FileViewInfo() { FileName = f.bhdFile.name, FileSize = f.bhdFile.size, FileContentType = f.bhdFile.bhdFileType.contentType, FileId = f.bhdFile.id, authorId = a.authorId, publisherId = a.publisherId, year = a.publishYear }).ToList(); tmpResource.fileInfo = files; } if (r.ResourceTypeId == 2) { List<LinkViewInfo> urls = (from u in dc.bhdResourceLinks where u.resourceId == r.ResourceId select new LinkViewInfo() { resourceURL = u.bhdLink.url }).ToList(); tmpResource.urlInfo = urls; } string tagRet = ""; var tags = (from t in dc.bhdResourceKeywords join k in dc.bhdKeywords on t.KeywordId equals k.id where k.isActive && t.Resourceid == id select k.value).ToList(); foreach (var item in tags) { tagRet = tagRet + item + ","; } if (tagRet.Length > 0) tagRet = tagRet.Remove(tagRet.Length - 1, 1); tmpResource.resourceInfo.ResourceTags = tagRet; } return tmpResource; }
public async Task<HttpResponseMessage> Post(int id, string type) { if (type != "website") { try { var result = Request.Content.ReadAsFormDataAsync(); Stream streamIn = await Request.Content.ReadAsStreamAsync(); StreamReader streamReader = new StreamReader(streamIn); string jsonstring = streamReader.ReadToEnd(); ResourceFile resFileRet; var temp = JsonConvert.DeserializeObject<FileData>(jsonstring); using (ResourcesDataContext dc = new ResourcesDataContext()) { //check if we have the file type int typeid = 0; var temptype = (from d in dc.bhdFileTypes where d.contentType == temp.fileType && d.isActive select d).FirstOrDefault(); if (temptype == null) { bhdFileType ft = new bhdFileType(); ft.contentType = temp.fileType; string extension = temp.fileName; int pos = extension.LastIndexOf('.'); ft.extension = extension.Substring(pos + 1, extension.Length - (pos + 1)); ft.isActive = true; dc.bhdFileTypes.InsertOnSubmit(ft); dc.SubmitChanges(); typeid = ft.id; } else { typeid = temptype.id; } bhdFile f = new bhdFile(); f.size = temp.fileSize; f.name = temp.fileName; f.isActive = true; f.fileTypeId = typeid; dc.bhdFiles.InsertOnSubmit(f); dc.SubmitChanges(); bhdFileData fd = new bhdFileData(); fd.fileId = f.id; fd.data = temp.fileData; dc.bhdFileDatas.InsertOnSubmit(fd); dc.SubmitChanges(); bhdResource res = (from d in dc.bhdResources where d.id == id select d).FirstOrDefault(); res.previewFileId = f.id; dc.SubmitChanges(); } return Request.CreateResponse(HttpStatusCode.OK, "success"); } catch (Exception) { return Request.CreateResponse(HttpStatusCode.BadRequest, "Something Broke. 400-1"); } } else { try { //this is for a website string websiteUrl = ""; var result = Request.Content.ReadAsFormDataAsync(); Stream streamIn = await Request.Content.ReadAsStreamAsync(); StreamReader streamReader = new StreamReader(streamIn); string jsonstring = streamReader.ReadToEnd(); var temp = JsonConvert.DeserializeObject<string>(jsonstring); websiteUrl = temp; using (ResourcesDataContext dc = new ResourcesDataContext()) { bhdLink l = new bhdLink(); l.url = websiteUrl; dc.bhdLinks.InsertOnSubmit(l); dc.SubmitChanges(); bhdResourceLink rl = new bhdResourceLink(); rl.linkId = l.linkId; rl.resourceId = id; dc.bhdResourceLinks.InsertOnSubmit(rl); dc.SubmitChanges(); } return Request.CreateResponse(HttpStatusCode.OK, "Success"); } catch (Exception) { return Request.CreateResponse(HttpStatusCode.BadRequest, "Something Broke. 400-9"); throw; } } }
public async Task<HttpResponseMessage> Post(int id) { try { var result = Request.Content.ReadAsFormDataAsync(); Stream streamIn = await Request.Content.ReadAsStreamAsync(); StreamReader streamReader = new StreamReader(streamIn); string jsonstring = streamReader.ReadToEnd(); // serialize that shit ResourceEditSend uploadModel; try { uploadModel = JsonConvert.DeserializeObject<ResourceEditSend>(jsonstring); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.BadRequest, ex); } //update the information using (ResourcesDataContext dc = new ResourcesDataContext()) { bhdResource tmpResource = dc.bhdResources.Single((x) => x.id == id); tmpResource.name = uploadModel.resourceInfo.ResourceName; tmpResource.description = uploadModel.resourceInfo.ResourceDescription; tmpResource.topicId = uploadModel.resourceInfo.ResourceTopicId; tmpResource.languageId = uploadModel.resourceInfo.ResourceLanguageId; dc.SubmitChanges(); if (uploadModel.URL != "") { } dc.bhdResourceKeywords.DeleteAllOnSubmit<bhdResourceKeyword>(dc.bhdResourceKeywords.Where((r)=>r.Resourceid == id)); foreach (var item in uploadModel.tagsInfo.tags) { var r = (from d in dc.bhdKeywords where d.isActive && d.value == item select d).FirstOrDefault(); if (r != null) { bhdResourceKeyword tmpResourceKeyword = new bhdResourceKeyword(); tmpResourceKeyword.KeywordId = r.id; tmpResourceKeyword.Resourceid = tmpResource.id; dc.bhdResourceKeywords.InsertOnSubmit(tmpResourceKeyword); dc.SubmitChanges(); } else { bhdKeyword tmpkeyword = new bhdKeyword(); tmpkeyword.value = item; tmpkeyword.isActive = true; dc.bhdKeywords.InsertOnSubmit(tmpkeyword); dc.SubmitChanges(); bhdResourceKeyword tmpResourceKeyword = new bhdResourceKeyword(); tmpResourceKeyword.KeywordId = tmpkeyword.id; tmpResourceKeyword.Resourceid = tmpResource.id; dc.bhdResourceKeywords.InsertOnSubmit(tmpResourceKeyword); dc.SubmitChanges(); } } } } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.BadRequest, "Something Broke. " + ex.Message + ". 400-3"); } return Request.CreateResponse(HttpStatusCode.OK, "Success"); }