This commit is contained in:
2016-08-03 23:31:05 +02:00
parent 01f36ddef6
commit 549f0df4b4
2 changed files with 294 additions and 0 deletions

View File

@ -0,0 +1,147 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using Yavsc.Models;
namespace Yavsc.Controllers
{
[Produces("application/json"),Authorize(Roles="Administrator")]
[Route("api/users")]
public class ApplicationUserApiController : Controller
{
private ApplicationDbContext _context;
public ApplicationUserApiController(ApplicationDbContext context)
{
_context = context;
}
// GET: api/ApplicationUserApi
[HttpGet]
public IEnumerable<ApplicationUser> GetApplicationUser()
{
return _context.Users;
}
// GET: api/ApplicationUserApi/5
[HttpGet("{id}", Name = "GetApplicationUser")]
public IActionResult GetApplicationUser([FromRoute] string id)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
ApplicationUser applicationUser = _context.Users.Single(m => m.Id == id);
if (applicationUser == null)
{
return HttpNotFound();
}
return Ok(applicationUser);
}
// PUT: api/ApplicationUserApi/5
[HttpPut("{id}")]
public IActionResult PutApplicationUser(string id, [FromBody] ApplicationUser applicationUser)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
if (id != applicationUser.Id)
{
return HttpBadRequest();
}
_context.Entry(applicationUser).State = EntityState.Modified;
try
{
_context.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!ApplicationUserExists(id))
{
return HttpNotFound();
}
else
{
throw;
}
}
return new HttpStatusCodeResult(StatusCodes.Status204NoContent);
}
// POST: api/ApplicationUserApi
[HttpPost]
public IActionResult PostApplicationUser([FromBody] ApplicationUser applicationUser)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
_context.Users.Add(applicationUser);
try
{
_context.SaveChanges();
}
catch (DbUpdateException)
{
if (ApplicationUserExists(applicationUser.Id))
{
return new HttpStatusCodeResult(StatusCodes.Status409Conflict);
}
else
{
throw;
}
}
return CreatedAtRoute("GetApplicationUser", new { id = applicationUser.Id }, applicationUser);
}
// DELETE: api/ApplicationUserApi/5
[HttpDelete("{id}")]
public IActionResult DeleteApplicationUser(string id)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
ApplicationUser applicationUser = _context.Users.Single(m => m.Id == id);
if (applicationUser == null)
{
return HttpNotFound();
}
_context.Users.Remove(applicationUser);
_context.SaveChanges();
return Ok(applicationUser);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_context.Dispose();
}
base.Dispose(disposing);
}
private bool ApplicationUserExists(string id)
{
return _context.Users.Count(e => e.Id == id) > 0;
}
}
}

View File

@ -0,0 +1,147 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using Yavsc.Models;
namespace Yavsc.Controllers
{
[Produces("application/json")]
[Route("api/blog")]
public class BlogApiController : Controller
{
private ApplicationDbContext _context;
public BlogApiController(ApplicationDbContext context)
{
_context = context;
}
// GET: api/BlogApi
[HttpGet]
public IEnumerable<Blog> GetBlogspot()
{
return _context.Blogspot.Where(b=>b.Visible).OrderByDescending(b=>b.Modified);
}
// GET: api/BlogApi/5
[HttpGet("{id}", Name = "GetBlog")]
public IActionResult GetBlog([FromRoute] long id)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
Blog blog = _context.Blogspot.Single(m => m.Id == id);
if (blog == null)
{
return HttpNotFound();
}
return Ok(blog);
}
// PUT: api/BlogApi/5
[HttpPut("{id}")]
public IActionResult PutBlog(long id, [FromBody] Blog blog)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
if (id != blog.Id)
{
return HttpBadRequest();
}
_context.Entry(blog).State = EntityState.Modified;
try
{
_context.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!BlogExists(id))
{
return HttpNotFound();
}
else
{
throw;
}
}
return new HttpStatusCodeResult(StatusCodes.Status204NoContent);
}
// POST: api/BlogApi
[HttpPost]
public IActionResult PostBlog([FromBody] Blog blog)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
_context.Blogspot.Add(blog);
try
{
_context.SaveChanges();
}
catch (DbUpdateException)
{
if (BlogExists(blog.Id))
{
return new HttpStatusCodeResult(StatusCodes.Status409Conflict);
}
else
{
throw;
}
}
return CreatedAtRoute("GetBlog", new { id = blog.Id }, blog);
}
// DELETE: api/BlogApi/5
[HttpDelete("{id}")]
public IActionResult DeleteBlog(long id)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
Blog blog = _context.Blogspot.Single(m => m.Id == id);
if (blog == null)
{
return HttpNotFound();
}
_context.Blogspot.Remove(blog);
_context.SaveChanges();
return Ok(blog);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_context.Dispose();
}
base.Dispose(disposing);
}
private bool BlogExists(long id)
{
return _context.Blogspot.Count(e => e.Id == id) > 0;
}
}
}