summaryrefslogtreecommitdiffstats
path: root/ComicRackWebViewer/BCRUser.cs
blob: eb388163db12fc839405ffe1b8d39fa0878d00a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using cYo.Projects.ComicRack.Engine;
using Nancy.Security;
using System;
using System.Collections.Generic;
using System.Data.SQLite;

namespace BCR
{
  public class BCRUser : IUserIdentity
  {
    // Cache the entire list of comic progress, so we only need to perform one SQL lookup instead of
    // one per comic. This speeds up the displaying of a list of comics.
    public Dictionary<Guid, ComicProgress> comicProgress = new Dictionary<Guid, ComicProgress>();

    public Guid homeListId;

    public UserSettings settings = new UserSettings();

    public IEnumerable<string> Claims { get; set; }

    public string FullName { get; set; }

    public int UserId { get; set; }

    public string UserName { get; set; }

    public ComicProgress GetComicProgress(Guid comicId)
    {
      ComicProgress progress;
      if (comicProgress.TryGetValue(comicId, out progress))
      {
        return progress;
      }

      return null;
    }

    public UserSettings GetSettings()
    {
      return settings;
    }

    public void Initialize()
    {
      settings.Load(this);

      comicProgress.Clear();
      using (SQLiteDataReader reader = Database.Instance.ExecuteReader("SELECT id, comic_id, current_page, last_page_read, date_last_read FROM comic_progress WHERE user_id = " + UserId + ";"))
      {
        while (reader.Read())
        {
          ComicProgress progress = new ComicProgress();
          progress.DatabaseId = reader.GetInt32(0);
          progress.Id = new Guid(reader.GetString(1));
          progress.CurrentPage = reader.GetInt32(2);
          progress.LastPageRead = reader.GetInt32(3);
          progress.DateLastRead = reader.GetString(4);
          comicProgress[progress.Id] = progress;
        }
      }
    }

    public void ResetComicProgress(Guid comicId)
    {
      comicProgress.Remove(comicId);
      Database.Instance.ExecuteNonQuery("DELETE FROM comic_progress WHERE comic_id = '" + comicId + "' AND user_id = " + UserId + ";");
    }

    public bool SetAccessLevel()
    {
      return false;
    }

    public bool SetPassword()
    {
      return false;
    }

    public void UpdateComicProgress(ComicBook comicBook, int currentPage)
    {
      if (comicBook == null)
        return;

      ComicProgress progress;
      if (comicProgress.TryGetValue(comicBook.Id, out progress))
      {
        if (currentPage > progress.LastPageRead)
          progress.LastPageRead = currentPage;

        progress.CurrentPage = currentPage;
        progress.DateLastRead = System.DateTime.Now.ToString("s");
        Database.Instance.ExecuteNonQuery("UPDATE comic_progress SET current_page = " + progress.CurrentPage + ", last_page_read = " + progress.LastPageRead + ", date_last_read = '" + progress.DateLastRead + "' WHERE id = " + progress.DatabaseId);
      }
      else
      {
        progress = new ComicProgress();
        progress.Id = comicBook.Id;
        progress.LastPageRead = currentPage;
        progress.CurrentPage = currentPage;
        progress.DateLastRead = System.DateTime.Now.ToString("s");

        Database.Instance.ExecuteNonQuery("INSERT INTO comic_progress (user_id, comic_id, current_page, last_page_read, date_last_read) VALUES(" + UserId + ", '" + progress.Id.ToString() + "', " + progress.CurrentPage + ", " + progress.LastPageRead + ", '" + progress.DateLastRead + "');");

        progress.DatabaseId = (int)Database.Instance.GetLastInsertRowId();
        comicProgress[comicBook.Id] = progress;
      }

      if (settings.use_comicrack_progress)
      {
        // Save the progess to the ComicRack database as well
        comicBook.CurrentPage = currentPage;
        if (comicBook.LastPageRead < currentPage)
          comicBook.LastPageRead = currentPage;

      }
    }

    public void UpdateSettings(UserSettings settings)
    {
      this.settings = settings;
      settings.Save(this);
    }
    /*
    public Guid GetHomeList()
    {
      object homeListId = Database.Instance.ExecuteScalar("SELECT home_list_id FROM user_settings WHERE user_id = " + UserId + " LIMIT 1;");
      if (homeListId != null)
      {
        Guid listId = new Guid(homeListId.ToString());

        // Check if list still exists, if not, return main library.
        var list = Program.Database.ComicLists.FindItem(listId);
        if (list == null)
        {
          // Home list no longer exists, return main library.
          string s = "cYo.Projects.ComicRack.Engine.Database.ComicLibraryListItem";
          ComicListItem item = Program.Database.ComicLists.GetItems<ComicListItem>(false).FirstOrDefault((ComicListItem cli) => cli.GetType().ToString() == s);
          if (item != null)
          {
            listId = item.Id;
          }
        }

        return listId;
      }
    }
    */
  }
}