blog tags api
This commit is contained in:
148
Yavsc/ApiControllers/BlogTagsApiController.cs
Normal file
148
Yavsc/ApiControllers/BlogTagsApiController.cs
Normal file
@ -0,0 +1,148 @@
|
||||
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;
|
||||
using Yavsc.Models.Relationship;
|
||||
|
||||
namespace Yavsc.Controllers
|
||||
{
|
||||
[Produces("application/json")]
|
||||
[Route("api/blogtags")]
|
||||
public class BlogTagsApiController : Controller
|
||||
{
|
||||
private ApplicationDbContext _context;
|
||||
|
||||
public BlogTagsApiController(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// GET: api/BlogTagsApi
|
||||
[HttpGet]
|
||||
public IEnumerable<BlogTag> GetTagsDomain()
|
||||
{
|
||||
return _context.TagsDomain;
|
||||
}
|
||||
|
||||
// GET: api/BlogTagsApi/5
|
||||
[HttpGet("{id}", Name = "GetBlogTag")]
|
||||
public async Task<IActionResult> GetBlogTag([FromRoute] long id)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return HttpBadRequest(ModelState);
|
||||
}
|
||||
|
||||
BlogTag blogTag = await _context.TagsDomain.SingleAsync(m => m.PostId == id);
|
||||
|
||||
if (blogTag == null)
|
||||
{
|
||||
return HttpNotFound();
|
||||
}
|
||||
|
||||
return Ok(blogTag);
|
||||
}
|
||||
|
||||
// PUT: api/BlogTagsApi/5
|
||||
[HttpPut("{id}")]
|
||||
public async Task<IActionResult> PutBlogTag([FromRoute] long id, [FromBody] BlogTag blogTag)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return HttpBadRequest(ModelState);
|
||||
}
|
||||
|
||||
if (id != blogTag.PostId)
|
||||
{
|
||||
return HttpBadRequest();
|
||||
}
|
||||
|
||||
_context.Entry(blogTag).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!BlogTagExists(id))
|
||||
{
|
||||
return HttpNotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return new HttpStatusCodeResult(StatusCodes.Status204NoContent);
|
||||
}
|
||||
|
||||
// POST: api/BlogTagsApi
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> PostBlogTag([FromBody] BlogTag blogTag)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return HttpBadRequest(ModelState);
|
||||
}
|
||||
|
||||
_context.TagsDomain.Add(blogTag);
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateException)
|
||||
{
|
||||
if (BlogTagExists(blogTag.PostId))
|
||||
{
|
||||
return new HttpStatusCodeResult(StatusCodes.Status409Conflict);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return CreatedAtRoute("GetBlogTag", new { id = blogTag.PostId }, blogTag);
|
||||
}
|
||||
|
||||
// DELETE: api/BlogTagsApi/5
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> DeleteBlogTag([FromRoute] long id)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return HttpBadRequest(ModelState);
|
||||
}
|
||||
|
||||
BlogTag blogTag = await _context.TagsDomain.SingleAsync(m => m.PostId == id);
|
||||
if (blogTag == null)
|
||||
{
|
||||
return HttpNotFound();
|
||||
}
|
||||
|
||||
_context.TagsDomain.Remove(blogTag);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return Ok(blogTag);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_context.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private bool BlogTagExists(long id)
|
||||
{
|
||||
return _context.TagsDomain.Count(e => e.PostId == id) > 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -23,7 +23,7 @@ namespace Yavsc.Controllers
|
||||
|
||||
// GET: api/PostTagsApi
|
||||
[HttpGet]
|
||||
public IEnumerable<PostTag> GetTagsDomain()
|
||||
public IEnumerable<BlogTag> GetTagsDomain()
|
||||
{
|
||||
return _context.TagsDomain;
|
||||
}
|
||||
@ -37,7 +37,7 @@ namespace Yavsc.Controllers
|
||||
return HttpBadRequest(ModelState);
|
||||
}
|
||||
|
||||
PostTag postTag = _context.TagsDomain.Single(m => m.PostId == id);
|
||||
BlogTag postTag = _context.TagsDomain.Single(m => m.PostId == id);
|
||||
|
||||
if (postTag == null)
|
||||
{
|
||||
@ -49,7 +49,7 @@ namespace Yavsc.Controllers
|
||||
|
||||
// PUT: api/PostTagsApi/5
|
||||
[HttpPut("{id}")]
|
||||
public IActionResult PutPostTag(long id, [FromBody] PostTag postTag)
|
||||
public IActionResult PutPostTag(long id, [FromBody] BlogTag postTag)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
@ -84,7 +84,7 @@ namespace Yavsc.Controllers
|
||||
|
||||
// POST: api/PostTagsApi
|
||||
[HttpPost]
|
||||
public IActionResult PostPostTag([FromBody] PostTag postTag)
|
||||
public IActionResult PostPostTag([FromBody] BlogTag postTag)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
@ -120,7 +120,7 @@ namespace Yavsc.Controllers
|
||||
return HttpBadRequest(ModelState);
|
||||
}
|
||||
|
||||
PostTag postTag = _context.TagsDomain.Single(m => m.PostId == id);
|
||||
BlogTag postTag = _context.TagsDomain.Single(m => m.PostId == id);
|
||||
if (postTag == null)
|
||||
{
|
||||
return HttpNotFound();
|
||||
|
@ -41,7 +41,7 @@ namespace Yavsc.Models
|
||||
// Add your customizations after calling base.OnModelCreating(builder);
|
||||
builder.Entity<Relationship.Contact>().HasKey(x => new { x.OwnerId, x.UserId });
|
||||
builder.Entity<GoogleCloudMobileDeclaration>().Property(x=>x.DeclarationDate).HasDefaultValueSql("LOCALTIMESTAMP");
|
||||
builder.Entity<PostTag>().HasKey(x=>new { x.PostId, x.TagId});
|
||||
builder.Entity<BlogTag>().HasKey(x=>new { x.PostId, x.TagId});
|
||||
builder.Entity<ApplicationUser>().HasMany<Connection>( c=>c.Connections );
|
||||
builder.Entity<ApplicationUser>().Property(u=>u.Avatar).HasDefaultValue(Constants.DefaultAvatar);
|
||||
builder.Entity<ApplicationUser>().Property(u=>u.DiskQuota).HasDefaultValue(Constants.DefaultFSQ);
|
||||
@ -186,7 +186,7 @@ namespace Yavsc.Models
|
||||
|
||||
public DbSet<Tag> Tags { get; set; }
|
||||
|
||||
public DbSet<PostTag> TagsDomain { get; set; }
|
||||
public DbSet<BlogTag> TagsDomain { get; set; }
|
||||
|
||||
public DbSet<EstimateTemplate> EstimateTemplates { get; set; }
|
||||
|
||||
@ -278,5 +278,7 @@ namespace Yavsc.Models
|
||||
public DbSet<HyperLink> Links { get; set; }
|
||||
|
||||
public DbSet<Period> Period { get; set; }
|
||||
|
||||
public DbSet<BlogTag> BlogTags { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -2,11 +2,14 @@ using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace Yavsc.Models.Relationship
|
||||
{
|
||||
public partial class PostTag
|
||||
public partial class BlogTag
|
||||
{
|
||||
[ForeignKey("PostId")]
|
||||
public virtual Blog Post { get; set; }
|
||||
public long PostId { get; set; }
|
||||
|
||||
[ForeignKey("TagId")]
|
||||
public virtual Tag Tag{ get; set; }
|
||||
public long TagId { get; set; }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user