Skip to content

Instantly share code, notes, and snippets.

@danielplawgo

danielplawgo/Product.cs

Last active Oct 23, 2018
Embed
What would you like to do?
Swagger - dokumentowanie REST API
/// <summary>
/// The product.
/// </summary>
public class Product
{
/// <summary>
/// Gets or sets the identifier.
/// </summary>
/// <value>
/// The identifier.
/// </value>
public int Id { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; set; }
/// <summary>
/// Gets or sets the category.
/// </summary>
/// <value>
/// The category.
/// </value>
public string Category { get; set; }
/// <summary>
/// Gets or sets the price.
/// </summary>
/// <value>
/// The price.
/// </value>
public decimal Price { get; set; }
}
/// <summary>
/// The products API.
/// </summary>
/// <seealso cref="System.Web.Http.ApiController" />
public class ProductsController : ApiController
{
private static List<Product> _products;
static ProductsController()
{
int id = 1;
_products = new Faker<Product>()
.RuleFor(o => o.Id, f => id++)
.RuleFor(p => p.Name, (f, p) => f.Commerce.ProductName())
.RuleFor(p => p.Category, (f, p) => f.Commerce.Categories(1).FirstOrDefault())
.RuleFor(p => p.Price, (f, p) => f.Random.Number(100, 100000) / 100M)
.Generate(10);
}
/// <summary>
/// Gets the products.
/// </summary>
/// <returns></returns>
public IEnumerable<Product> Get()
{
return _products;
}
/// <summary>
/// Gets the product by identifier.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns></returns>
public Product Get(int id)
{
return _products.FirstOrDefault(p => p.Id == id);
}
/// <summary>
/// Add the product.
/// </summary>
/// <param name="product">The product.</param>
public void Post([FromBody]Product product)
{
product.Id = _products.Select(p => p.Id).Max() + 1;
_products.Add(product);
}
/// <summary>
/// Update the product.
/// </summary>
/// <param name="id">The identifier.</param>
/// <param name="product">The product.</param>
public void Put(int id, [FromBody]Product product)
{
var existingProduct = _products.FirstOrDefault(p => p.Id == id);
if (existingProduct != null)
{
existingProduct.Category = product.Category;
existingProduct.Name = product.Name;
existingProduct.Price = product.Price;
}
}
/// <summary>
/// Deletes the product.
/// </summary>
/// <param name="id">The identifier.</param>
public void Delete(int id)
{
var product = _products.FirstOrDefault(p => p.Id == id);
if (product != null)
{
_products.Remove(product);
}
}
}
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
namespace SwaggerExample.Api
{
public class SwaggerConfig
{
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "SwaggerExample.Api");
c.IncludeXmlComments(string.Format(@"{0}\bin\{1}.xml", System.AppDomain.CurrentDomain.BaseDirectory, thisAssembly.GetName().Name));
})
.EnableSwaggerUi(c =>
{
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.