Skip to content

Commit

Permalink
Merge pull request #3 from amg262/post-fix
Browse files Browse the repository at this point in the history
pr
  • Loading branch information
amg262 authored Apr 5, 2024
2 parents 03bde96 + ac6ce8e commit 677fb75
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/GatewayService/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
},
"postRead": {
"ClusterId": "post",
"AuthorizationPolicy": "default",
// "AuthorizationPolicy": "default",
"Match": {
"Path": "/post/{**catch-all}",
"Methods": [
Expand All @@ -160,7 +160,7 @@
"PathPattern": "api/post/{**catch-all}"
}
]
},
}
// "paymentWrite": {
// "ClusterId": "payment",
// "AuthorizationPolicy": "default",
Expand Down
35 changes: 35 additions & 0 deletions src/PostService/Controllers/PostController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Mvc;
using MongoDB.Entities;
using PostService.Models;

namespace PostService.Controllers;

[ApiController]
[Route("api/[controller]")]
public class PostController : ControllerBase
{
[HttpPost("create")]
public async Task<IActionResult> Post([FromBody] Post post)
{
if (post == null)
{
return BadRequest("Post cannot be null.");
}

await post.SaveAsync(); // MongoDB.Entities simplifies the save operation

return CreatedAtAction(nameof(Post), new {id = post.ID}, post);
}

[HttpGet("{id:length(24)}")]
public async Task<IActionResult> Get(string id)
{
var post = await DB.Find<Post>().OneAsync(id);
if (post == null)
{
return NotFound($"Post with ID {id} not found.");
}

return Ok(post);
}
}
2 changes: 1 addition & 1 deletion src/PostService/Data/DbInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class DbInitializer
/// </remarks>
public static async Task InitDb(WebApplication app)
{
await DB.InitAsync("SearchDb", MongoClientSettings
await DB.InitAsync("PostDb", MongoClientSettings
.FromConnectionString(app.Configuration.GetConnectionString("MongoDbConnection")));

// await DB.Index<Item>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ namespace PostService.Models;
/// </summary>
public class Post : Entity
{
public Guid? Id { get; set; }
public Guid? Guid { get; set; }
public string? Title { get; set; }
public string? Description { get; set; }
public string? Author { get; set; }
public string? ImageUrl { get; set; }
public string? Category { get; set; }
public string? Status { get; set; }
public DateTime? CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
public DateTime? CreatedAt { get; set; } = DateTime.UtcNow;
public DateTime? UpdatedAt { get; set; } = DateTime.UtcNow;
}
2 changes: 1 addition & 1 deletion src/PostService/PostService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="MassTransit.RabbitMQ" Version="8.0.16" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="7.0.8" />
Expand All @@ -15,7 +16,6 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Controllers\" />
<ProjectReference Include="..\Contracts\Contracts.csproj" />

</ItemGroup>
Expand Down

0 comments on commit 677fb75

Please sign in to comment.