Advertisement
Zgragselus

profile.ts

Sep 10th, 2023
1,334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. This file is subject to the terms and conditions defined in
  4. file 'LICENSE', which is part of this source code package.
  5.  
  6. © OtteIT s.r.o.
  7. All Rights Reserved.
  8.  
  9. Author: Vilem Otte <[email protected]>
  10.  
  11. */
  12.  
  13. import { ErrorFactory } from "../util/error";
  14. import { Progress } from "../util/progress";
  15. import { System } from "../util/system";
  16. import { Url } from "../util/url";
  17.  
  18. /**
  19.  * User's profile class
  20.  *
  21.  * This class takes care of requests for user
  22.  */
  23. export class UserProfile
  24. {
  25.     /**
  26.      * User's ID
  27.      */
  28.     public id: number;
  29.  
  30.     /**
  31.      * User's login name
  32.      */
  33.     public login_name: string;
  34.  
  35.     /**
  36.      * User's email
  37.      */
  38.     public email: string;
  39.  
  40.     /**
  41.      * User's password (never read, only for update/creation)
  42.      */
  43.     public password: string;
  44.  
  45.     /**
  46.      * Default constructor
  47.      * @param id User's ID
  48.      * @param login_name Login name
  49.      * @param email Email address
  50.      * @param password Password
  51.      */
  52.     constructor(id: number, login_name: string = "", email: string = "", password: string = "")
  53.     {
  54.         this.id = id;
  55.         this.login_name = login_name;
  56.         this.email = email;
  57.         this.password = password;
  58.     }
  59.  
  60.     /**
  61.      * Create user record
  62.      * @param progress Instance of Progress class where request progress is written
  63.      * @returns Promise object with result of creation (or error data on failure)
  64.      */
  65.     public Send(progress: Progress)
  66.     {
  67.         var json = { "login_name": this.login_name, "email": this.email, "password": this.password };
  68.  
  69.         return Url.FetchJson(System.BaseURL() + "api/v1/user/create.php", true, json, progress);
  70.     }
  71.  
  72.     /**
  73.      * Deleta user record
  74.      * @param progress Instance of Progress class where request progress is written
  75.      * @returns Promise object with result of deletion
  76.      */
  77.     public Delete(progress: Progress)
  78.     {
  79.         var json = { "id": this.id };
  80.  
  81.         return Url.FetchJson(System.BaseURL() + "api/v1/user/delete.php", true, json, progress);
  82.     }
  83.  
  84.     /**
  85.      * Update user record
  86.      * @param progress Instance of Progress class where request progress is written
  87.      * @returns Promise object with result of update
  88.      */
  89.     public Update(progress: Progress)
  90.     {
  91.         var json = { "id": this.id, "login_name": this.login_name, "email": this.email, "password": this.password };
  92.  
  93.         return Url.FetchJson(System.BaseURL() + "api/v1/user/update.php", true, json, progress);
  94.     }
  95.  
  96.     /**
  97.      * Get user/users record/records
  98.      * @param progress Instance of Progress class where request progress is written
  99.      * @returns Promise object with array of user's records (or error data on failure)
  100.      */
  101.     public Request(progress: Progress)
  102.     {    
  103.         var json = {};
  104.        
  105.         if (this.id > 0)
  106.         {
  107.             json = { "id": this.id };
  108.         }
  109.  
  110.         return Url.FetchJson(System.BaseURL() + "api/v1/user/read.php", false, json, progress);
  111.     }
  112.  
  113.     /**
  114.      * Update user's avatar to a new file
  115.      * @param user_id User ID for which avatar is modified
  116.      * @param file New file to update
  117.      * @returns Promise object with update result
  118.      */
  119.     public async UpdateAvatar(file: File, progress: Progress)
  120.     {
  121.         var media_id = 0;
  122.  
  123.         var json_user = { "id": this.id };
  124.         var res_json = {};
  125.         await Url.FetchJson(System.BaseURL() + "api/v1/user/read.php", false, json_user, progress).then(function(res)
  126.         {
  127.             if (res.result == 200 && res.user.length > 0)
  128.             {
  129.                 media_id = res.user[0].avatar;
  130.             }
  131.             else
  132.             {
  133.                 ErrorFactory.Show(res.status, res.statusText);
  134.             }
  135.         });
  136.  
  137.         if (media_id > 0)
  138.         {
  139.             var json_delete = { "id": media_id };
  140.  
  141.             await Url.FetchJson(System.BaseURL() + "api/v1/media/delete.php", true, json_delete, progress).then(function(res)
  142.             {
  143.                 if (res.result != 200)
  144.                 {
  145.                     ErrorFactory.Show(res.status, res.statusText);
  146.                 }
  147.             });
  148.         }
  149.  
  150.         await Url.FetchSendFile(System.BaseURL() + "api/v1/media/upload.php", true, file, progress).then(function(res)
  151.         {
  152.             if (res.result == 200)
  153.             {
  154.                 media_id = res.media;
  155.             }
  156.             else
  157.             {
  158.                 ErrorFactory.Show(res.status, res.statusText);
  159.             }
  160.         });
  161.  
  162.         var json = { "id": this.id, "avatar": media_id };
  163.         return Url.FetchJson(System.BaseURL() + "api/v1/user/update.php", true, json, progress);
  164.     }
  165.  
  166.     /**
  167.      * Get list of permissions for user
  168.      * @param user_id User ID for which permission list is requested
  169.      * @returns Promise object with list of permissions for user (or error data on failure)
  170.      */
  171.     public Permission(progress: Progress)
  172.     {
  173.         var json = { "user_id": this.id };
  174.        
  175.         return Url.FetchJson(System.BaseURL() + "api/v1/permission/read.php", true, json, progress);
  176.     }
  177.  
  178.     /**
  179.      * Update permission record
  180.      * @param progress Instance of Progress class where request progress is written
  181.      * @param permission_id Permission record ID to modify
  182.      * @param permission_role New permission role
  183.      * @param permission_level New permission level
  184.      * @returns Promise object with result of permission update
  185.      */
  186.     public UpdatePermission(progress: Progress, permission_id: number, permission_role: string, permission_level: number)
  187.     {
  188.         var json = { "id":  permission_id, "role": permission_role, "level": permission_level };
  189.  
  190.         return Url.FetchJson(System.BaseURL() + "api/v1/permission/update.php", true, json, progress);
  191.     }
  192.  
  193.     /**
  194.      * Add permission record for user
  195.      * @param progress Instance of Progress class where request progress is written
  196.      * @param permission_role Permission role to add
  197.      * @param permission_level Permission level to set
  198.      * @returns Promise object with result of permission add
  199.      */
  200.     public AddPermission(progress: Progress, permission_role: string, permission_level: number)
  201.     {
  202.         var json = { "user_id": this.id, "role": permission_role, "level": permission_level };
  203.  
  204.         return Url.FetchJson(System.BaseURL() + "api/v1/permission/create.php", true, json, progress);
  205.     }
  206. }
  207.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement