This commit is contained in:
parent
9e8d00d78f
commit
bc6bebea06
4 changed files with 55 additions and 4 deletions
|
|
@ -15,10 +15,14 @@ namespace Yavsc.Blogs.Controllers
|
|||
public class BlogApiController : Controller
|
||||
{
|
||||
private readonly BlogSpotService blogSpotService;
|
||||
private readonly ILogger<BlogApiController> logger;
|
||||
|
||||
public BlogApiController(BlogSpotService blogSpotService)
|
||||
public BlogApiController(
|
||||
BlogSpotService blogSpotService,
|
||||
ILoggerFactory loggerFactory)
|
||||
{
|
||||
this.blogSpotService = blogSpotService;
|
||||
this.logger = loggerFactory.CreateLogger<BlogApiController>();
|
||||
}
|
||||
|
||||
// GET: api/v1/blogspot
|
||||
|
|
@ -60,6 +64,7 @@ namespace Yavsc.Blogs.Controllers
|
|||
var blog = await ReadBlogRequestAsync();
|
||||
if (blog is null)
|
||||
{
|
||||
logger.LogWarning("PUT /api/v1/blogspot/{id} failed to read blog payload from request body or multipart form.", id);
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
|
|
@ -79,6 +84,7 @@ namespace Yavsc.Blogs.Controllers
|
|||
result.MemberNames.FirstOrDefault() ?? string.Empty,
|
||||
result.ErrorMessage ?? "Invalid value.");
|
||||
}
|
||||
logger.LogWarning("PUT /api/v1/blogspot/{id} failed validation: {errors}", id, ModelState);
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
|
|
@ -107,16 +113,27 @@ namespace Yavsc.Blogs.Controllers
|
|||
var existing = await blogSpotService.GetBlogPostAsync(id);
|
||||
if (existing == null)
|
||||
{
|
||||
logger.LogWarning("PUT /api/v1/blogspot/{id} failed: blog post not found.", id);
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await blogSpotService.Modify(User, blog);
|
||||
blogSpotService.AttachFiles(files, User.GetUserId(), id);
|
||||
logger.LogInformation("PUT /api/v1/blogspot/{id} modified successfully.", id);
|
||||
if (files.Count > 0)
|
||||
{
|
||||
logger.LogInformation("PUT /api/v1/blogspot/{id} attaching {fileCount} files.", id, files.Count);
|
||||
blogSpotService.AttachFiles(files, User.GetUserId(), id);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogInformation("PUT /api/v1/blogspot/{id} no files to attach.", id);
|
||||
}
|
||||
}
|
||||
catch (AuthorizationFailureException)
|
||||
{
|
||||
logger.LogWarning("PUT /api/v1/blogspot/{id} failed: authorization failure.", id);
|
||||
return Challenge();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ using Yavsc.Models;
|
|||
using Yavsc.Services;
|
||||
using Yavsc.Server.Helpers;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.Extensions.Options;
|
||||
namespace Yavsc.Blogs;
|
||||
|
||||
internal class Program
|
||||
|
|
@ -22,6 +23,8 @@ internal class Program
|
|||
|
||||
var services = builder.Services;
|
||||
|
||||
var siteSection = builder.Configuration.GetSection("Site");
|
||||
services.Configure<SiteSettings>(siteSection);
|
||||
// MvcBuilder
|
||||
builder.Services
|
||||
.AddAuthorization(options =>
|
||||
|
|
@ -79,8 +82,17 @@ internal class Program
|
|||
options.SetDefaultCulture(supportedCultures[0])
|
||||
.AddSupportedCultures(supportedCultures)
|
||||
.AddSupportedUICultures(supportedCultures);
|
||||
})
|
||||
.Configure<SiteSettings>(settings =>
|
||||
{
|
||||
if (settings.Blog == null)
|
||||
{
|
||||
settings.Blog = "blog";
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
// App startup
|
||||
using (var app = builder.Build())
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue