• About project
  • Code documentation
Show / Hide Table of Contents
  • Project structure
  • Resources
  • Services

Services

To add a new service follow the next steps:

  1. Add the link to API service to EduCATS/Networking/Links.cs file:
public static string GetRecomendations => $"{Servers.Current}/Tests/GetRecomendations";
  1. 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);
}
  1. Add the service callback to EduCATS/Data/DataAccessCallbacks.cs:
static async Task<object> getRecommendationsCallback(int subjectId, int userId) => await AppServices.GetRecommendations(subjectId, userId);
  1. 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; }
}
  1. Specify caching key in EduCATS/Constants/GlobalConsts.cs:
public const string DataGetRecommendationsKey = "GET_RECOMMENDATIONS_KEY";
  1. 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>;
}
  1. Use it inside your ViewModel:
var recommendations = await DataAccess.GetRecommendations(CurrentSubject.Id, AppUserData.UserId);
In this article
Back to top Copyright (C) EduCATS. Generated by DocFX.