Skip to content
This repository has been archived by the owner on Jul 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from UA-CS491-591/Develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Matthew York committed May 10, 2014
2 parents 144ccbf + f5f117d commit cf133f1
Show file tree
Hide file tree
Showing 27 changed files with 2,695 additions and 96 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,4 @@ UpgradeLog*.htm

# Microsoft Fakes
FakesAssemblies/
Project2/WebServices/Web.config
2 changes: 1 addition & 1 deletion Project2/WebServices/App_Start/WebApiConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Expand Down
28 changes: 28 additions & 0 deletions Project2/WebServices/Category.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace WebServices
{
using System;
using System.Collections.Generic;

public partial class Category
{
public Category()
{
this.Stories = new HashSet<Story>();
}

public System.Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }

public virtual ICollection<Story> Stories { get; set; }
}
}
57 changes: 54 additions & 3 deletions Project2/WebServices/Controllers/API/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,66 @@
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebServices.Models.Dtos;

namespace WebServices.Controllers
{
public class AccountController : ApiController
{
[HttpGet]
public string helloWorld()
private const String readerAccessToken = "b7a2ac80-67a7-41bb-a7ff-8e6574b0bdf2";
private const String writerAccessToken = "b6a5b4f7-3e7f-4f2e-aca3-932f0a2a7f1d";

[HttpPost]
public DtoLoginResponse login(DtoLogin dtoLogin)
{
if (dtoLogin != null)
{
if (!String.IsNullOrEmpty(dtoLogin.password) && !String.IsNullOrEmpty(dtoLogin.username))
{
DtoLoginResponse response = new DtoLoginResponse();
response.user = DtoUser.UserForUserName(dtoLogin.username);

//Check password
if (dtoLogin.username.Equals("zbarnes") && dtoLogin.password.Equals("password"))
{
response.accessToken = writerAccessToken;
}
else if (dtoLogin.username.Equals("lgoodwin") && dtoLogin.password.Equals("password"))
{
response.accessToken = writerAccessToken;
}
else if (dtoLogin.username.Equals("jskorsky") && dtoLogin.password.Equals("password"))
{
response.accessToken = writerAccessToken;
}
else if (dtoLogin.username.Equals("funderwood") && dtoLogin.password.Equals("password"))
{
response.accessToken = readerAccessToken;
}
else if (dtoLogin.username.Equals("cunderwood") && dtoLogin.password.Equals("password"))
{
response.accessToken = readerAccessToken;
}
else if (dtoLogin.username.Equals("dstamper") && dtoLogin.password.Equals("password"))
{
response.accessToken = readerAccessToken;
}

return response;
}
}

return null;
}

public static Boolean isValidWriter(string token)
{
return token.Equals(writerAccessToken);
}

public static Boolean isValidReader(string token)
{
return "hello world!";
return token.Equals(readerAccessToken);
}
}
}
37 changes: 37 additions & 0 deletions Project2/WebServices/Controllers/API/CategoryController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebServices.Models.Dtos;

namespace WebServices.Controllers.API
{
public class CategoryController : ApiController
{
StoryModelContainer db = new StoryModelContainer();

[HttpGet]
public List<DtoCategory> categories(string token)
{
if (AccountController.isValidReader(token) || AccountController.isValidWriter(token))
{
List<Category> categories = db.Categories.ToList();

if (categories != null)
{
List<DtoCategory> dtoCategories = new List<DtoCategory>();
foreach (Category category in categories)
{
dtoCategories.Add(DtoCategory.dtoFromCategory(category));
}

return dtoCategories;
}
}

return null;
}
}
}
204 changes: 204 additions & 0 deletions Project2/WebServices/Controllers/API/StoryController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebServices.Models.Dtos;

namespace WebServices.Controllers.API
{
public class StoryController : ApiController
{
StoryModelContainer db = new StoryModelContainer();

[HttpGet]
public DtoStory byId(Guid token, Guid storyId)
{
if (AccountController.isValidReader(token.ToString()) || AccountController.isValidWriter(token.ToString()))
{
//Fetch story
Story story = db.Stories.Where(s => s.Id.Equals(storyId)).SingleOrDefault();

if (story != null)
{
return DtoStory.dtoFromStory(story);
}
}

return null;
}

[HttpGet]
public List<DtoStory> recent(string token)
{
if (AccountController.isValidReader(token) || AccountController.isValidWriter(token))
{
List<Story> stories = db.Stories.OrderByDescending(s => s.DatePublished).Take(20).ToList();

if (stories != null)
{
List<DtoStory> dtoStories = new List<DtoStory>();

foreach (Story story in stories)
{
dtoStories.Add(DtoStory.lightDtoFromStory(story));
}

return dtoStories;
}
}

return null;
}

[HttpGet]
public List<DtoStory> byCategory(Guid token, Guid categoryId)
{
if (AccountController.isValidReader(token.ToString()) || AccountController.isValidWriter(token.ToString()))
{
Category category = db.Categories.Where(c => c.Id == categoryId).SingleOrDefault();

if (category != null)
{
List<Story> stories = category.Stories.OrderByDescending(s => s.DatePublished).ToList();

if (stories != null)
{

List<DtoStory> dtoStories = new List<DtoStory>();

foreach (Story story in stories)
{
dtoStories.Add(DtoStory.lightDtoFromStory(story));
}

return dtoStories;
}
else
{
return new List<DtoStory>();
}
}

}

return null;
}

[HttpGet]
public List<DtoStory> byAuthor(Guid token, string authorId)
{
if (AccountController.isValidReader(token.ToString()) || AccountController.isValidWriter(token.ToString()))
{

List<Story> stories = db.Stories.Where(s => s.authorId.Equals(authorId)).OrderByDescending(s => s.DatePublished).ToList();

if (stories != null)
{
List<DtoStory> dtoStories = new List<DtoStory>();

foreach (Story story in stories)
{
dtoStories.Add(DtoStory.lightDtoFromStory(story));
}

return dtoStories;
}
}

return null;
}

[HttpGet]
public List<DtoStory> search(string token, string searchString)
{
if (AccountController.isValidReader(token) || AccountController.isValidWriter(token))
{
List<Story> stories = db.Stories.Where(s => s.Title.Contains(searchString) || s.Subtitle.Contains(searchString) || s.Body.Contains(searchString)).OrderByDescending(s => s.DatePublished).Take(20).ToList();

if (stories != null)
{
List<DtoStory> dtoStories = new List<DtoStory>();

foreach (Story story in stories)
{
dtoStories.Add(DtoStory.lightDtoFromStory(story));
}

return dtoStories;
}
}

return null;
}

[HttpPost]
public DtoStory add(DtoAddStory dtoAddStory)
{
if (AccountController.isValidWriter(dtoAddStory.accessToken))
{
Category category = db.Categories.Where(c => c.Id.Equals(dtoAddStory.categoryId)).SingleOrDefault();

if (category != null)
{
Story story = DtoStory.newStoryFromDto(dtoAddStory);

if (story != null)
{
category.Stories.Add(story);
db.SaveChanges();

return DtoStory.dtoFromStory(story);
}
}
}

return null;
}

[HttpPut]
public Boolean edit(DtoEditStory dto){

if (AccountController.isValidWriter(dto.accessToken))
{
Story story = db.Stories.Where(s => s.Id.Equals(dto.story.storyId)).SingleOrDefault();

if (story != null)
{
story.Title = dto.story.title;
story.Subtitle = dto.story.subtitle;
story.Body = dto.story.body;
story.Lat = dto.story.lat;
story.Lng = dto.story.lng;
story.DateUpdated = DateTime.UtcNow;

db.SaveChanges();
return true;
}
}

return false;
}

[HttpDelete]
public Boolean delete(string token, Guid storyId)
{

if (AccountController.isValidWriter(token))
{
Story story = db.Stories.Where(s => s.Id.Equals(storyId)).SingleOrDefault();

if (story != null)
{
db.Stories.Remove(story);

db.SaveChanges();
return true;
}
}

return false;
}
}
}
12 changes: 12 additions & 0 deletions Project2/WebServices/Models/Dtos/DtoAccessToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebServices.Models.Dtos
{
public class DtoAccessToken
{
public string accessToken { get; set; }
}
}
18 changes: 18 additions & 0 deletions Project2/WebServices/Models/Dtos/DtoAddStory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebServices.Models.Dtos
{
public class DtoAddStory : DtoAccessToken
{
public String title { get; set; }
public String subtitle { get; set; }
public String body { get; set; }
public Guid authorId { get; set; }
public double? lat { get; set; }
public double? lng { get; set; }
public Guid categoryId { get; set; }
}
}
Loading

0 comments on commit cf133f1

Please sign in to comment.