Services
To add a new service follow the next steps:
- Add the link to API service to
EduCATS/Networking/Links.cs
file:
public static string GetRecomendations => $"{Servers.Current}/Tests/GetRecomendations";
- Add the app service method to
EduCATS/Networking/AppServices/AppServices.cs
:
public static async Task<object> GetRecommendations(int subjectId, int userId)
{
return await AppServicesController.Request($"{Links.GetRecomendations}?subjectId={subjectId}&studentId={userId}");
}
2.1. If you need POST
request you should create the model for body in EduCATS/Networking/Models
and use it like:
static string postRequest(string username)
{
var usernameBody = new UsernameModel {
Username = username
};
var body = JsonController.ConvertObjectToJson(userLogin);
return await AppServicesController.Request(Links.PostRequest, body);
}
- Add the service callback to
EduCATS/Data/DataAccessCallbacks.cs
:
static async Task<object> getRecommendationsCallback(int subjectId, int userId) => await AppServices.GetRecommendations(subjectId, userId);
- Add the model for API result in
EduCATS/Data/Models/{API_name}.cs
:
public class RecommendationModel
{
[JsonProperty("IsTest")]
public bool IsTest { get; set; }
[JsonProperty("Id")]
public int Id { get; set; }
[JsonProperty("Text")]
public string Text { get; set; }
}
- Specify caching key in
EduCATS/Constants/GlobalConsts.cs
:
public const string DataGetRecommendationsKey = "GET_RECOMMENDATIONS_KEY";
- Add the method to
EduCATS/Data/DataAccess.cs
:
public async static Task<List<RecommendationModel>> GetRecommendations(int subjectId, int userId)
{
var dataAccess = new DataAccess<RecommendationModel>(
"recommendations_fetch_error", // localized error key
getRecommendationsCallback(subjectId, userId), // callback specified in DataAccessCallbacks.cs
$"{GlobalConsts.DataGetRecommendationsKey}/{subjectId}/{userId}"); // specified caching key; this parameter is optional - no caching will be provided if null
return getDataObject(dataAccess, await dataAccess.GetList()) as List<RecommendationModel>;
}
- Use it inside your ViewModel:
var recommendations = await DataAccess.GetRecommendations(CurrentSubject.Id, AppUserData.UserId);