/ Published in: C#
Recommendations API implementation using Azure Mobile Services
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using Microsoft.WindowsAzure.Mobile.Service; using Microsoft.WindowsAzure.Storage.Table; using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure; using giventocode.m.DataObjects; namespace giventocode.m.Controllers { public class BlogRecommendationsController : ApiController { public ApiServices Services { get; set; } // GET api/BlogRecommendations [AuthorizeLevel(Microsoft.WindowsAzure.Mobile.Service.Security.AuthorizationLevel.Application)] public IEnumerable<BlogItem> Get(string title) { var resultsTable = GetCloudTable(CloudConfigurationManager.GetSetting("RecommendationsDataTable")); Services.Log.Info(CloudConfigurationManager.GetSetting("RecommendationsDataTable")); Services.Log.Info(title); var cluster =resultsTable.CreateQuery<BlogEntryResult>() .Where<BlogEntryResult>(b => b.Title == title) .Select<BlogEntryResult, string> (r => r.Assignments) .FirstOrDefault<string>(); if (cluster != null) { return resultsTable.CreateQuery<BlogEntryResult>() .Where<BlogEntryResult>(b => b.Assignments == cluster && b.Title != title) .Select<BlogEntryResult, BlogItem> } return null; } private CloudTable GetCloudTable(string name) { var cloudStorageAccount = CloudStorageAccount.DevelopmentStorageAccount; CloudStorageAccount.TryParse(CloudConfigurationManager.GetSetting("StorageAcct"), out cloudStorageAccount); var tClient = cloudStorageAccount.CreateCloudTableClient(); var tref = tClient.GetTableReference(name); tref.CreateIfNotExists(); return tref; } } }