Skip to content

Instantly share code, notes, and snippets.

View ntakouris's full-sized avatar
🤖
Building robots

Theodoros Ntakouris ntakouris

🤖
Building robots
View GitHub Profile
namespace Tweetbook.Contracts.V1
{
public static class ApiRoutes
{
public const string Root = "api";
public const string Version = "v1";
public const string Base = Root + "/" + Version;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Tweetbook.Installers
{
public interface IInstaller
{
void InstallServices(IServiceCollection services, IConfiguration configuration);
}
}
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));
[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());
[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());
[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);
private readonly IPostService _postService;
public PostsController(IPostService postService) {
_postService = postService;
}
[HttpGet(ApiRoutes.Posts.Get)]
public IActionResult Get([FromRoute] Guid postId)
{
public interface IPostService
{
List<Post> GetPosts();
Post GetPostById(Guid postId);
}
var post = new Post
{
Id = postId,
Name = request.Name
};
var updated = _postService.UpdatePost(post);
if (updated)
return Ok(post);
var post = new Post
{
Id = postId,
Name = request.Name
};
var updated = _postService.UpdatePost(post);
if (updated)
return Ok(post);