87 lines
2.8 KiB
C#
87 lines
2.8 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Net;
|
|||
|
using System.Net.Http;
|
|||
|
using System.Web.Http;
|
|||
|
using Microsoft.AspNet.Identity;
|
|||
|
using PagedList;
|
|||
|
using Weee.Models;
|
|||
|
using Weee.Service;
|
|||
|
using Weee.Supports;
|
|||
|
|
|||
|
namespace Weee.Controllers
|
|||
|
{
|
|||
|
public class ProductController : ApiControllerBase
|
|||
|
{
|
|||
|
private readonly WeeeProductDataService service;
|
|||
|
public ProductController(WeeeProductDataService s)
|
|||
|
:base(s)
|
|||
|
{
|
|||
|
service = s;
|
|||
|
}
|
|||
|
|
|||
|
[Route("api/Product/GetByPage/{page?}/{searchString?}")]
|
|||
|
[HttpGet]
|
|||
|
public Object GetByPage(int page = 1, string searchString = "")
|
|||
|
{
|
|||
|
var pageSize = 10;
|
|||
|
var products = service.GetProducts();
|
|||
|
var dataFilter = new DataFilter();
|
|||
|
var filteredProducts = dataFilter.FilterProduct(products, searchString);
|
|||
|
var pagedProducts = filteredProducts.ToPagedList(page, pageSize);
|
|||
|
return new
|
|||
|
{
|
|||
|
products = pagedProducts,
|
|||
|
totalPage = pagedProducts.PageCount
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
[Route("api/Product/Get/{id}")]
|
|||
|
[HttpGet]
|
|||
|
public Product Get(int id)
|
|||
|
{
|
|||
|
return service.GetProduct(id);
|
|||
|
}
|
|||
|
|
|||
|
[Route("api/Product/GetAll")]
|
|||
|
[HttpGet]
|
|||
|
public IEnumerable<Product> GetAll()
|
|||
|
{
|
|||
|
return service.GetProducts();
|
|||
|
}
|
|||
|
//CFT-22排序
|
|||
|
[Route("api/Product/GetProductsByOrder/{orderBy}/{orderDir}")]
|
|||
|
[HttpGet]
|
|||
|
public IEnumerable<Product> GetAll(string orderBy, string orderDir)
|
|||
|
{
|
|||
|
return service.GetProductsByOrder(orderBy, orderDir);
|
|||
|
}
|
|||
|
|
|||
|
[Route("api/Product/Save")]
|
|||
|
[HttpPost]
|
|||
|
public HttpResponseMessage Save(Product toBeSave)
|
|||
|
{
|
|||
|
if (ModelState.IsValid)
|
|||
|
{
|
|||
|
if (service.CheckNameExist(toBeSave.Name, toBeSave.ID))
|
|||
|
{
|
|||
|
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "產品\"" + toBeSave.Name + "\"已存在");
|
|||
|
}
|
|||
|
var userid = User.Identity.GetUserId();
|
|||
|
toBeSave.UserId = userid;
|
|||
|
//if (! string.IsNullOrEmpty( toBeSave.PhotoUrl))//DL-5
|
|||
|
//{
|
|||
|
// if (toBeSave.PhotoUrl.Contains("Browser_Local"))
|
|||
|
// {
|
|||
|
// toBeSave.PhotoUrl = toBeSave.PhotoUrl.Substring(toBeSave.PhotoUrl.IndexOf("Browser_Local") - 1);
|
|||
|
// }
|
|||
|
//}
|
|||
|
service.SaveProduct(toBeSave);
|
|||
|
return Request.CreateResponse(HttpStatusCode.OK, toBeSave);
|
|||
|
}
|
|||
|
|
|||
|
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|