Assignment 1
Task 1.1
Create a REST endpoint that takes from a list of all clients only their email adresses and returns a list of email adresses
path is /api/clients/emails
[Route("api/clients/emails")]
[HttpGet]
public List<string> GetEmails()
{
return GetDb()
.Select(x => x.Email)
.ToList();
}
Task 1.2
Create a REST endpoint that returns a list consisting of the clients names whose name starts with a letter submitted as a parameter
path is /api/clients/{firstLetter}
[Route("api/clients/{firstLetter:alpha}")]
[HttpGet]
public List<string> GetNames(char firstLetter)
{
return GetDb()
.Where(x => Char.ToLower(x.Name[0]) == Char.ToLower(firstLetter))
.Select(x => x.Name)
.ToList();
}
Task 1.3
Create a REST endpoint that returns a list consisting of names and surnames of clients whose name starts with a letter given as a parameter and their surname also starts with a letter given as a parameter
path is /api/clients/firstLetters
[Route("api/clients/firstLetters")]
[HttpGet]
public List<List<string>> GetNamesSurnames([FromUri]char nameChar, [FromUri]char surnameChar)
{
return GetDb()
.Where(x => Char.ToLower(x.Name[0]) == Char.ToLower(nameChar) &&
Char.ToLower(x.Surname[0]) == Char.ToLower(surnameChar))
.Select(x => new List<string> { x.Name, x.Surname })
.ToList();
}
Task 1.4
Create a REST endpoint that returns a list of client names sorted in the order given as a parameter.
path /api/clients/sort/{order}
[Route("api/clients/sort/{order:regex(asc|desc)}")]
[HttpGet]
public List<string> GetClientNamesOrdered(string order)
{
List<BankClientModel> clients = GetDb();
switch (order)
{
case "asc":
{
return clients
.Select(x => x.Name)
.OrderBy(name => name)
.ToList();
}
case "desc":
{
return clients
.Select(x => x.Name)
.OrderByDescending(name => name)
.ToList();
}
default:
return null;
}
}
Assignment 2
Task 1.1
Create a REST endpoint that will set the bonitet field of each client in the list of clients. The bonitet field will be set to āPā (positive) if the client is younger than 65 years, or āNā (negative) if the client is older than 65 years.
path /api/clients/*bonitet*/
add the attributes datum rodjenja
and bonitet
to the BankClientModel
[Route("api/clients/bonitet")]
[HttpPut]
public List<BankClientModel> UpdateBonitet()
{
List<BankClientModel> clients = GetDb();
foreach (var b in clients)
{
TimeSpan ts = DateTime.Now - b.DatumRodjenja;
int age = ts.Days / 365;
if (age < 65)
{
b.Bonitet = 'P';
}
else
{
b.Bonitet = 'N';
}
}
return clients;
}
Task 1.2
Create a REST endpoint that deletes the client from the list of clients if the clients does not have one of the ime
, prezime
, email
attributes (fields) set.
path /api/clients/delete
[Route("api/clients/delete")]
[HttpDelete]
public List<BankClientModel> DeleteNonComplete()
{
List<BankClientModel> clients = GetDb();
clients.RemoveAll(x => x.Name == null || x.Surname == null || x.Email == null);
return clients.ToList();
}
Task 1.3
Create a REST endpoint that returns the count of clients in the list of clients that are younger than the age given as a parameter.
path /clients/countLess/{years}
[Route("api/clients/countLess/{years:int}")]
[HttpGet]
public List<BankClientModel> GetClientsWithGivenAge(int years)
{
List<BankClientModel> clients = GetDb();
Func<DateTime, int, bool> CompareAges = (a, b) =>
{
TimeSpan ts = DateTime.Now - a;
int age = ts.Days / 365;
return age < b ? true : false;
};
return clients.Where(x => CompareAges(x.DatumRodjenja, years)).ToList();
}
Task 1.4
Create a REST endpoint that will return the average age of clients from the list of clients.
path /api/clients/averageYears
[Route("api/clients/averageYears")]
[HttpGet]
public double GetAverageAge()
{
List<BankClientModel> clients = GetDb();
DateTime now = DateTime.Now;
int ClientAge(DateTime a)
{
TimeSpan ts = now - a;
return ts.Days / 365;
}
return clients.Average(x => ClientAge(x.DatumRodjenja));
}
Assignment 3
Task 1.1
Create a REST endpoint that will enable changing the adress of the client.
path /api/clients/changelocation/{clientId}
add a new attribute, grad
to the BankClientModel
class
the new adress should be forwarded as a FromUri
[Route("api/clients/changelocation/{clientId}")]
[HttpPut]
public BankClientModel ChangeClientLocation(int clientId, [FromUri]string location)
{
List<BankClientModel> clients = GetDb();
BankClientModel client = clients.Find(x => x.Id == clientId);
if (client != null)
{
client.Grad = location;
}
return client;
}
Task 1.2
Create a REST endpoint that returns the clients of the bank that live in the city that was given as a parameter
path /api/clients/from/{city}
[Route("api/clients/from/{city}")]
[HttpGet]
public List<BankClientModel> GetClientsFromCity(string city)
{
return GetDb().Where(x => x.Grad.ToLower() == city).ToList();
}