I want to expose 3 endpoints("Add to coin", "Get total amount of coins", "Reset coins"), but i am struggling if this implementation should be done on the controller side because i have an Interface, need some help and this is my logic so far.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CoinJarAPI.Interface
{
interface ICoinJar
{
void AddCoin(ICoin coin);
decimal GetTotalAmount();
void Reset();
}
public interface ICoin
{
decimal Amount { get; set; }
decimal Volume { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using CoinJarAPI.Models;
namespace CoinJarAPI.Controllers
{
public class CoinJarController : ApiController
{
// GET: api/CoinJar
public IEnumerable<CoinJarModel> Get()
{
var coinJarList = new List<CoinJarModel>();
for (int i = 0; i < 10; i++)
{
var coinjarModel = new CoinJarModel
{
// volume, Amount, GetTotalAmount.
};
}
return coinJarList;
}
// GET: api/CoinJar/5
public string Get(int id)
{
return "value";
}
}
}
Depends on what you want to follow. If your implementation is pure 100% rest then:
CoinJarController
endpoints You should define the following:
// GET: api/CoinJar
// GET: api/CoinJar/5
// POST: api/CoinJar/
and it should be used to insert new CoinJar
items // PUT: api/CoinJar/
and it should be used to update CoinJar
items // DELETE: api/CoinJar/
and it should be used to delete CoinJar
itemsNow, depending on wether or not a coin should be considered a resource on it's own, then you could declare the following
// GET: api/CoinJar/{id}/Coin/
// GET: api/CoinJar/{id}/Coin/{CoinId}
// POST: api/CoinJar/{id}/Coin/{CoinId}
which would insert a new coin in the CoinJar etc You can also use the PUT
version of the CoinJar
to just update (AddCoin/Reset etc) the coins in one go. It all depends on your needs.
All your logic and interfaces, should be used on another layer, the business one. This way you can implement your interfaces and use them accordingly from your controllers or any other kind of client.
Check these excellent api guidelines from Microsoft for more design information.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.