Untitled diff

Erstellt Diff läuft nie ab
59 Entfernungen
112 Zeilen
59 Hinzufügungen
111 Zeilen
using CSCAssignment.Models;
using CSCAssignment.Models;
using CSCAssignment.Filters;
using System;
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
using System.Net;
using System.Net;
using System.Net.Http;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http;


namespace CSCAssignment.Controllers
namespace CSCAssignment.Controllers
{
{
public class ProductsV2Controller : ApiController
public class ProductsV3Controller : ApiController
{
{
static readonly IProductRepository repository = new ProductRepository();
static readonly IProductRepository repository = new ProductRepository();

//Version 3
//[Authorize]
[HttpGet]
[HttpGet]
[Route("api/v2/products")]
[Route("api/v3/products")]
public IEnumerable<Product> GetAllProducts() //get all
public IEnumerable<Product> GetAllProductsFromRepository()
{
{
return repository.GetAll();
return repository.GetAll();


}
}
//GET , POST, PUT , DELETE
//Route constraints let you restrict how the parameters in the route template are matched.
//The general syntax is "{parameter:constraint}".
//Constraints on URL parameters

//We can even restrict the template placeholder to the type of parameter it can have.
//For example, we can restrict that the request will be only served if the id is greater than 2.
//Otherwise the request will not work. For this, we will apply multiple constraints in the same request:


//Type of the parameter id must be an integer.
//id should be greater than 2.
//http://localhost:9000/api/v3/products/1 404 error code
[HttpGet]
[HttpGet]
[Route("api/v2/products/{id:int}", Name = "getProductById")]
[Route("api/v3/products/{id:int:min(2)}", Name = "getProductByIdv3")]

public HttpResponseMessage GetProduct(int id) //get by id, changed to HttpResponseMessage to return message when error encounter.
public Product retrieveProductfromRepository(int id)
{
{
Product item = repository.Get(id);
Product item = repository.Get(id);

HttpResponseMessage response = null;
if (item == null)
if (item == null)
{
{
response = Request.CreateResponse(HttpStatusCode.NotFound, "Invalid id. Invalid get request.");
throw new HttpResponseException(HttpStatusCode.NotFound);
}
else
{
response = Request.CreateResponse<Product>(HttpStatusCode.OK, item);
}
}
return response;
return item;

}
}
//returns products based on category


[HttpGet]
[HttpGet]
[Route("api/v2/products")]
[Route("api/v3/products", Name = "getProductByCategoryv3")]
//http://localhost:9000/api/v2/products/category?category=3
//http://localhost:9000/api/v3/products?category=
//http://localhost:9000/api/v3/products?category=Groceries

public IEnumerable<Product> GetProductsByCategory(string category) //get by category
public IEnumerable<Product> GetProductsByCategory(string category)
{
{
return repository.GetAll().Where(
return repository.GetAll().Where(
p => string.Equals(p.Category, category, StringComparison.OrdinalIgnoreCase));
p => string.Equals(p.Category, category, StringComparison.OrdinalIgnoreCase));

}
}
//Post

[ValidateModel]

//Response code: By default, the Web API framework sets the response status code to 200 (OK).
//But according to the HTTP/1.1 protocol, when a POST request results in the creation of a resource, the server should reply with status 201 (Created).
//Location: When the server creates a resource, it should include the URI of the new resource in the Location header of the response.

[HttpPost]
[HttpPost]
[Route("api/v2/products")]
[Route("api/v3/products")]
public HttpResponseMessage PostProduct(Product item) //create product
public HttpResponseMessage PostProduct(Product item)
{
{

if (ModelState.IsValid)
if (ModelState.IsValid)
{
{
item = repository.Add(item);
item = repository.Add(item);
var response = Request.CreateResponse<Product>(HttpStatusCode.Created, item);
var response = Request.CreateResponse<Product>(HttpStatusCode.Created, item);

// Generate a link to the new product and set the Location header in the response.

string uri = Url.Link("getProductById", new { id = item.Id });
string uri = Url.Link("getProductByIdv3", new { id = item.Id });
response.Headers.Location = new Uri(uri);
response.Headers.Location = new Uri(uri);
return response;
return response;
}
}
else
else
{
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
}
}
}


//Put

[ValidateModel]