Posted By

giventocode on 09/28/14


Tagged


Versions (?)

BlogRecommendationsController


 / Published in: C#
 

URL: http://giventocode.com/build-a-recommendations-system-for-your-blog-or-web-site-using-azure-machine-learning-and-azure-mobile-services#

Recommendations API implementation using Azure Mobile Services

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Web.Http;
  7. using Microsoft.WindowsAzure.Mobile.Service;
  8. using Microsoft.WindowsAzure.Storage.Table;
  9. using Microsoft.WindowsAzure.Storage;
  10. using Microsoft.WindowsAzure;
  11. using giventocode.m.DataObjects;
  12.  
  13. namespace giventocode.m.Controllers
  14. {
  15. public class BlogRecommendationsController : ApiController
  16. {
  17. public ApiServices Services { get; set; }
  18.  
  19.  
  20. // GET api/BlogRecommendations
  21. [AuthorizeLevel(Microsoft.WindowsAzure.Mobile.Service.Security.AuthorizationLevel.Application)]
  22. public IEnumerable<BlogItem> Get(string title)
  23. {
  24. var resultsTable = GetCloudTable(CloudConfigurationManager.GetSetting("RecommendationsDataTable"));
  25.  
  26. Services.Log.Info(CloudConfigurationManager.GetSetting("RecommendationsDataTable"));
  27.  
  28. Services.Log.Info(title);
  29.  
  30. var cluster =resultsTable.CreateQuery<BlogEntryResult>()
  31. .Where<BlogEntryResult>(b => b.Title == title)
  32. .Select<BlogEntryResult, string>
  33. (r => r.Assignments)
  34. .FirstOrDefault<string>();
  35.  
  36. if (cluster != null)
  37. {
  38. return resultsTable.CreateQuery<BlogEntryResult>()
  39. .Where<BlogEntryResult>(b => b.Assignments == cluster && b.Title != title)
  40. .Select<BlogEntryResult, BlogItem>
  41. (r => new BlogItem() { Title = r.Title, Link = r.Link });
  42. }
  43.  
  44. return null;
  45. }
  46. private CloudTable GetCloudTable(string name)
  47. {
  48. var cloudStorageAccount = CloudStorageAccount.DevelopmentStorageAccount;
  49. CloudStorageAccount.TryParse(CloudConfigurationManager.GetSetting("StorageAcct"), out cloudStorageAccount);
  50. var tClient = cloudStorageAccount.CreateCloudTableClient();
  51. var tref = tClient.GetTableReference(name);
  52. tref.CreateIfNotExists();
  53.  
  54. return tref;
  55.  
  56. }
  57.  
  58. }
  59. }

Report this snippet  

You need to login to post a comment.