This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Tweetbook.Contracts.V1 | |
{ | |
public static class ApiRoutes | |
{ | |
public const string Root = "api"; | |
public const string Version = "v1"; | |
public const string Base = Root + "/" + Version; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
namespace Tweetbook.Installers | |
{ | |
public interface IInstaller | |
{ | |
void InstallServices(IServiceCollection services, IConfiguration configuration); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var installers = typeof(Startup).Assembly.ExportedTypes.Where(x => | |
typeof(IInstaller).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract).Select(Activator.CreateInstance).Cast<IInstaller>().ToList(); | |
installers.ForEach(installer => installer.InstallServices(services, configuration)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost(ApiRoutes.Posts.Create)] | |
public Task<IActionResult> Create([FromBody] Post post) | |
{ | |
if (string.IsNullOrEmpty(post.Id) | |
post.Id = Guid.NewGuid().ToString(); | |
_posts.AddPost(post); | |
var baseUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.ToUriComponent()}"; | |
var locationUri = baseUrl + "/" + ApiRoutes.Posts.Get.Replace("{postId}", post.Id.ToString()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost(ApiRoutes.Posts.Create)] | |
public Task<IActionResult> Create([FromBody] Post post) | |
{ | |
if (string.IsNullOrEmpty(post.Id) | |
post.Id = Guid.NewGuid().ToString(); | |
_posts.AddPost(post); | |
var baseUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.ToUriComponent()}"; | |
var locationUri = baseUrl + "/" + ApiRoutes.Posts.Get.Replace("{postId}", post.Id.ToString()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost(ApiRoutes.Posts.Create)] | |
public Task<IActionResult> Create([FromBody] CreatePostRequest postRequest) | |
{ | |
var post = new Post {Id = postRequest.Id}; // <-- this is called mapping | |
if (string.IsNullOrEmpty(post.Id) | |
post.Id = Guid.NewGuid().ToString(); | |
_posts.AddPost(post); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private readonly IPostService _postService; | |
public PostsController(IPostService postService) { | |
_postService = postService; | |
} | |
[HttpGet(ApiRoutes.Posts.Get)] | |
public IActionResult Get([FromRoute] Guid postId) | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IPostService | |
{ | |
List<Post> GetPosts(); | |
Post GetPostById(Guid postId); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var post = new Post | |
{ | |
Id = postId, | |
Name = request.Name | |
}; | |
var updated = _postService.UpdatePost(post); | |
if (updated) | |
return Ok(post); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var post = new Post | |
{ | |
Id = postId, | |
Name = request.Name | |
}; | |
var updated = _postService.UpdatePost(post); | |
if (updated) | |
return Ok(post); |