déplacé ou commenté

This commit is contained in:
Paul Schneider 2019-01-25 17:28:42 +00:00
commit 9507e7b960
18 changed files with 5 additions and 1 deletions

View file

@ -0,0 +1,148 @@
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using Yavsc.Models;
using Yavsc.Models.Blog;
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<BlogPost> GetBlogspot()
{
return _context.Blogspot.Where(b=>b.Visible).OrderByDescending(b=>b.UserModified);
}
// GET: api/BlogApi/5
[HttpGet("{id}", Name = "GetBlog")]
public IActionResult GetBlog([FromRoute] long id)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
BlogPost 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] BlogPost blog)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
if (id != blog.Id)
{
return HttpBadRequest();
}
_context.Entry(blog).State = EntityState.Modified;
try
{
_context.SaveChanges(User.GetUserId());
}
catch (DbUpdateConcurrencyException)
{
if (!BlogExists(id))
{
return HttpNotFound();
}
else
{
throw;
}
}
return new HttpStatusCodeResult(StatusCodes.Status204NoContent);
}
// POST: api/BlogApi
[HttpPost]
public IActionResult PostBlog([FromBody] Models.Blog.BlogPost blog)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
_context.Blogspot.Add(blog);
try
{
_context.SaveChanges(User.GetUserId());
}
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);
}
BlogPost blog = _context.Blogspot.Single(m => m.Id == id);
if (blog == null)
{
return HttpNotFound();
}
_context.Blogspot.Remove(blog);
_context.SaveChanges(User.GetUserId());
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;
}
}
}

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;
using Yavsc.Models.Blog;
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;
}
}
}

View file

@ -0,0 +1,161 @@
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using Yavsc.Models;
using Yavsc.Models.Blog;
namespace Yavsc.Controllers
{
[Produces("application/json")]
[Route("api/blogcomments")]
public class CommentsApiController : Controller
{
private ApplicationDbContext _context;
public CommentsApiController(ApplicationDbContext context)
{
_context = context;
}
// GET: api/CommentsApi
[HttpGet]
public IEnumerable<Comment> GetComment()
{
return _context.Comment;
}
// GET: api/CommentsApi/5
[HttpGet("{id}", Name = "GetComment")]
public async Task<IActionResult> GetComment([FromRoute] long id)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
Comment comment = await _context.Comment.SingleAsync(m => m.Id == id);
if (comment == null)
{
return HttpNotFound();
}
return Ok(comment);
}
// PUT: api/CommentsApi/5
[HttpPut("{id}")]
public async Task<IActionResult> PutComment([FromRoute] long id, [FromBody] Comment comment)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
if (id != comment.Id)
{
return HttpBadRequest();
}
_context.Entry(comment).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync(User.GetUserId());
}
catch (DbUpdateConcurrencyException)
{
if (!CommentExists(id))
{
return HttpNotFound();
}
else
{
throw;
}
}
return new HttpStatusCodeResult(StatusCodes.Status204NoContent);
}
// POST: api/CommentsApi
[HttpPost]
public async Task<IActionResult> PostComment([FromBody] Comment comment)
{
if (!ModelState.IsValid)
{
return new BadRequestObjectResult(ModelState);
}
if (!User.IsInRole(Constants.AdminGroupName))
{
if (User.GetUserId()!=comment.AuthorId) {
ModelState.AddModelError("Content","Vous ne pouvez pas poster au nom d'un autre.");
return new BadRequestObjectResult(ModelState);
}
}
_context.Comment.Add(comment);
try
{
await _context.SaveChangesAsync(User.GetUserId());
}
catch (DbUpdateException)
{
if (CommentExists(comment.Id))
{
return new HttpStatusCodeResult(StatusCodes.Status409Conflict);
}
else
{
throw;
}
}
return CreatedAtRoute("GetComment", new { id = comment.Id }, comment);
}
// DELETE: api/CommentsApi/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteComment([FromRoute] long id)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
Comment comment = await _context.Comment.SingleAsync(m => m.Id == id);
if (comment == null)
{
return HttpNotFound();
}
RemoveRecursive(comment);
await _context.SaveChangesAsync(User.GetUserId());
return Ok(comment);
}
private void RemoveRecursive (Comment comment)
{
var children = _context.Comment.Where(c=>c.ParentId==comment.Id).ToList();
foreach (var child in children) {
RemoveRecursive(child);
}
_context.Comment.Remove(comment);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_context.Dispose();
}
base.Dispose(disposing);
}
private bool CommentExists(long id)
{
return _context.Comment.Count(e => e.Id == id) > 0;
}
}
}

View file

@ -0,0 +1,113 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Mvc;
using Yavsc.Helpers;
using Yavsc.Models;
namespace Yavsc.ApiControllers
{
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Yavsc.Abstract.FileSystem;
using Yavsc.Exceptions;
using Yavsc.Models.FileSystem;
public class FSQuotaException : Exception {
}
[Authorize,Route("api/fs")]
public class FileSystemApiController : Controller
{
ApplicationDbContext dbContext;
private IAuthorizationService AuthorizationService;
private ILogger logger;
public FileSystemApiController(ApplicationDbContext context,
IAuthorizationService authorizationService,
ILoggerFactory loggerFactory)
{
AuthorizationService = authorizationService;
dbContext = context;
logger = loggerFactory.CreateLogger<FileSystemApiController>();
}
[HttpGet()]
public IActionResult Get()
{
return GetDir(null);
}
[HttpGet("{*subdir}")]
public IActionResult GetDir(string subdir="")
{
if (subdir !=null)
if (!subdir.IsValidYavscPath())
return new BadRequestResult();
var files = User.GetUserFiles(subdir);
return Ok(files);
}
[HttpPost("{*subdir}")]
public IActionResult Post(string subdir="")
{
string destDir = null;
List<FileRecievedInfo> received = new List<FileRecievedInfo>();
InvalidPathException pathex = null;
try {
destDir = User.InitPostToFileSystem(subdir);
} catch (InvalidPathException ex) {
pathex = ex;
}
if (pathex!=null) {
logger.LogError($"invalid sub path: '{subdir}'.");
return HttpBadRequest(pathex);
}
logger.LogInformation($"Receiving files, saved in '{destDir}' (specified as '{subdir}').");
var uid = User.GetUserId();
var user = dbContext.Users.Single(
u => u.Id == uid
);
int i=0;
logger.LogInformation($"Receiving {Request.Form.Files.Count} files.");
foreach (var f in Request.Form.Files)
{
var item = user.ReceiveUserFile(destDir, f);
dbContext.SaveChanges(User.GetUserId());
received.Add(item);
logger.LogInformation($"Received '{item.FileName}'.");
if (item.QuotaOffensed)
break;
i++;
};
return Ok(received);
}
[HttpDelete]
public async Task <IActionResult> Delete (string id)
{
var user = dbContext.Users.Single(
u => u.Id == User.GetUserId()
);
InvalidPathException pathex = null;
string root = null;
try {
root = User.InitPostToFileSystem(id);
} catch (InvalidPathException ex) {
pathex = ex;
}
if (pathex!=null)
return new BadRequestObjectResult(pathex);
user.DeleteUserFile(id);
await dbContext.SaveChangesAsync(User.GetUserId());
return Ok(new { deleted=id });
}
}
}