Skip to content

Instantly share code, notes, and snippets.

@AlexMata260
Created July 11, 2018 01:43
Show Gist options
  • Save AlexMata260/049e0bf67f2ef63ea0743de099f5f376 to your computer and use it in GitHub Desktop.
Save AlexMata260/049e0bf67f2ef63ea0743de099f5f376 to your computer and use it in GitHub Desktop.
Sample API controller. C# ASP.NET
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace Controllers.Api
{
[RoutePrefix("api/posts")]
public class PostsApiController : BaseApiController
{
// Dependency Injection
private IPostsService _postsService; //_postsService is a field a.k.a a var
public PostsApiController (IPostsService postsService)
{
_postsService = postsService;
}
//public interface IPostsService
[Route(""), HttpPost]
public HttpResponseMessage Create(PostCreateRequest model)
{
if (model == null)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Payload cannot be empty");
}
if (model.GameId != null && model.ArtworkId == null || model.GameId == null && model.ArtworkId != null)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "GameId and ArtworkId combination is not valid. Both must be null or not null");
}
if (!ModelState.IsValid)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
else
{
ItemResponse<int> response = new ItemResponse<int>();
response.Item = _postsService.Create(model);
return Request.CreateResponse(HttpStatusCode.OK, response);
}
}
[Route("{id:int}"), HttpPut]
public HttpResponseMessage Update(PostUpdateRequest model, int id)
{
if (model == null)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Payload cannot be empty");
}
if (model.Id != id)
{
ModelState.AddModelError("id", "Id is not valid");
}
if ((model.GameId != null && model.ArtworkId == null) || (model.GameId == null && model.ArtworkId != null))
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "GameId and ArtworkId combination is not valid. Both must be null or not null");
}
if (!ModelState.IsValid)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
}
_postsService.Update(model);
SuccessResponse response = new SuccessResponse();
return Request.CreateResponse(HttpStatusCode.OK, response);
}
[Route(""), HttpGet]
// Declaring another method GetAll
public HttpResponseMessage GetAll()
{
ItemsResponse<Post> response = new ItemsResponse<Post>();
response.Items = _postsService.GetAll();
return Request.CreateResponse(HttpStatusCode.OK, response);
}
// Delete
[Route("{id:int}"), HttpDelete]
public HttpResponseMessage Delete(int id)
{
_postsService.Delete(id);
SuccessResponse response = new SuccessResponse();
return Request.CreateResponse(HttpStatusCode.OK, response);
}
// Edit
[Route("{id:int}"), HttpGet]
public HttpResponseMessage GetById(int id)
{
Post post = _postsService.GetById(id);
ItemResponse<Post> response = new ItemResponse<Post>();
response.Item = post;
return Request.CreateResponse(HttpStatusCode.OK, response);
}
[Route(""), HttpGet]
public HttpResponseMessage GetRecentByUserId(int userId, string order, int limit)
{
if (order != "dateCreated")
{
return Request.CreateResponse(HttpStatusCode.BadRequest, "Order is not valid");
}
ItemsResponse<Post> response = new ItemsResponse<Post>();
response.Items = _postsService.GetRecentByUserId(userId, order, limit);
return Request.CreateResponse(HttpStatusCode.OK, response);
}
[Route(""), HttpGet]
public HttpResponseMessage GetRecent(string order, int limit)
{
if (order != "dateCreated")
{
return Request.CreateResponse(HttpStatusCode.BadRequest, "Order is not valid");
}
ItemsResponse<Post> response = new ItemsResponse<Post>();
response.Items = _postsService.GetRecent(order, limit);
return Request.CreateResponse(HttpStatusCode.OK, response);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment