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 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 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 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 ResourceViewPayload Get(int id)
        {
            //this is to get a resource
            ResourceViewPayload tmpResource = new ResourceViewPayload();
            ResourceList tmpList = new 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
                         join rf in dc.bhdResourceFormats on d.id equals rf.resourceId
                         where d.id == id
                         select new { ResourceName = d.name, ResourceDescription = d.description, ResourceUpload = d.uploadDate, ResourceId = d.id, ResourceLanguage = l.name, ResourceTopic = top.name, ResourceType = typ.name, ResourceFormat = rf.bhdFormat.name, ResourceTopicId = top.id, PreviewFileId = d.previewFileId}).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.ResourceType = r.ResourceType;
                    tmpList.ResourceFormat = r.ResourceFormat;
                    
                }
                if (r.PreviewFileId != null)
                    tmpList.PreviewFileId = (int)r.PreviewFileId;
                
                //get rating score 
                var rate = (from d in dc.bhdResourceRatings
                            where d.resourceId == id
                            select d);
                if (rate != null)
                {
                    int ratingAve = 0;
                    tmpResource.ratingCount = rate.Count();
                    foreach (var item in rate)
                    {
                        ratingAve = ratingAve + item.rating;
                    }
                    if (ratingAve > 0 && rate.Count() > 0)
                    {
                        ratingAve = (ratingAve / rate.Count());
                    }
                    tmpResource.averageRating = ratingAve;
                }
                else
                {
                    tmpResource.ratingCount = 0;
                    tmpResource.averageRating = 0;
                }
                // resource comments
                List<Comment> resourceComments = dc.bhdResourceComments.Where((x) => x.resourceId == id && x.isActive.Value).Select(x => new Comment { userId = x.userId, resourceId = x.resourceId, commentDate = x.commentDate.Value, comment = x.comment, commentId = x.id, active = x.isActive.Value }).ToList();
                tmpResource.comments = resourceComments;
                tmpResource.resourceInfo = tmpList;
                //fix for the topic
                var rtopic = dc.sps_getResourceTopicListById(r.ResourceTopicId, true);
                foreach (var item in rtopic)
                {
                    if (tmpList.ResourceTopic != null)
                    {
                        tmpList.ResourceTopic = tmpList.ResourceTopic + " > " + item.name;
                    }
                    else
                    {
                        tmpList.ResourceTopic = item.name;
                    }
                }

                List<FileViewInfo> files = (from f in dc.bhdResourceFiles
                                            where f.resourceId == r.ResourceId
                                            select new FileViewInfo() { FileName = f.bhdFile.name, FileSize = f.bhdFile.size, FileContentType = f.bhdFile.bhdFileType.extension, FileId = f.bhdFile.id }).ToList();
                foreach (var item in files)
                {
                    if (item.FileSize != null && item.FileSize != 0)
                    {
                        item.FileSize = item.FileSize / 1000;
                    }
                    string fileContentType = item.FileContentType;
                    if (fileContentType != null && fileContentType != "")
                    {
                        fileContentType = fileContentType.Substring((fileContentType.IndexOf('/') + 1), fileContentType.Length - (fileContentType.IndexOf('/') + 1));
                    }
                    item.FileContentType = fileContentType;
                }
                tmpResource.fileInfo = files;

                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 ResourceListPayload Get(int id, string topic, string orderby, string order)
 {
     int topicId = int.Parse(topic);
     ResourceListPayload payload = new ResourceListPayload();
     payload.resourceList = new List<ResourceList>();
     using (ResourcesDataContext dc = new ResourcesDataContext())
     {
         //var r = dc.sps_getResourceList(false, id + 1, 20, search); //name, rating or topic
         var r = dc.sps_getResourceListByTopic(topicId, true, id + 1, 20, orderby, order);
         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;
 }