Created
January 18, 2019 14:46
-
-
Save danielplawgo/0dbe492e98e8f0a9a787ec4902705f62 to your computer and use it in GitHub Desktop.
Postman - testowanie API
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 class Product | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public string Category { get; set; } | |
public decimal Price { get; set; } | |
} |
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 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); | |
} | |
public IEnumerable<Product> Get() | |
{ | |
return _products; | |
} | |
public Product Get(int id) | |
{ | |
return _products.FirstOrDefault(p => p.Id == id); | |
} | |
public Product Post([FromBody]Product product) | |
{ | |
product.Id = _products.Select(p => p.Id).Max() + 1; | |
_products.Add(product); | |
return product; | |
} | |
public Product 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; | |
} | |
return existingProduct; | |
} | |
public void Delete(int id) | |
{ | |
var product = _products.FirstOrDefault(p => p.Id == id); | |
if (product != null) | |
{ | |
_products.Remove(product); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment