|
| 1 | +using SQLite; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Collections.ObjectModel; |
| 5 | +using System.Collections.Specialized; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace CutCode |
| 12 | +{ |
| 13 | + public class DataBaseManager : IDataBase |
| 14 | + { |
| 15 | + public ObservableCollection<CodeBoxModel> AllCodes { get; set; } |
| 16 | + private SQLiteConnection _db; |
| 17 | + private readonly IThemeService themeService; |
| 18 | + public DataBaseManager(IThemeService _themeService) |
| 19 | + { |
| 20 | + var path = "DataBase.db"; |
| 21 | + _db = new SQLiteConnection(path); |
| 22 | + _db.CreateTable<CodeTable>(); |
| 23 | + |
| 24 | + themeService = _themeService; |
| 25 | + |
| 26 | + AllCodes = new ObservableCollection<CodeBoxModel>(); |
| 27 | + |
| 28 | + var codes = _db.Query<CodeTable>("SELECT * From CodeTable"); |
| 29 | + foreach (var c in codes) |
| 30 | + { |
| 31 | + AllCodes.Add(new CodeBoxModel(c.Id, c.title, c.desc, c.isFav, c.lang, c.code, c.timestamp, themeService)); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private int GetIndex(CodeBoxModel code) |
| 36 | + { |
| 37 | + int ind = 0; |
| 38 | + foreach(var c in AllCodes) |
| 39 | + { |
| 40 | + if (c.id == code.id) break; |
| 41 | + ind++; |
| 42 | + } |
| 43 | + return ind; |
| 44 | + } |
| 45 | + |
| 46 | + public event EventHandler PropertyChanged; |
| 47 | + public void AllCodesPropertyChanged() => PropertyChanged?.Invoke(this, EventArgs.Empty); |
| 48 | + |
| 49 | + |
| 50 | + public CodeBoxModel AddCode(string title, string desc, string code, string langType) |
| 51 | + { |
| 52 | + var time = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(); |
| 53 | + |
| 54 | + var dbCode = new CodeTable |
| 55 | + { |
| 56 | + title = title, |
| 57 | + desc = desc, |
| 58 | + code = code, |
| 59 | + lang = langType, |
| 60 | + isFav = false, |
| 61 | + timestamp = time |
| 62 | + }; |
| 63 | + _db.Insert(dbCode); |
| 64 | + |
| 65 | + int id = (int)SQLite3.LastInsertRowid(_db.Handle); |
| 66 | + var codeModel = new CodeBoxModel(id, title, desc, false, langType, code, time, themeService); |
| 67 | + AllCodes.Add(codeModel); |
| 68 | + AllCodesPropertyChanged(); |
| 69 | + |
| 70 | + return codeModel; |
| 71 | + } |
| 72 | + |
| 73 | + public bool EditCode(CodeBoxModel code) |
| 74 | + { |
| 75 | + try |
| 76 | + { |
| 77 | + var dbCode = _db.Query<CodeTable>("select * from CodeTable where Id = ?", code.id).FirstOrDefault(); |
| 78 | + if(dbCode is not null) |
| 79 | + { |
| 80 | + dbCode.title = code.title; |
| 81 | + dbCode.desc = code.desc; |
| 82 | + dbCode.code = code.code; |
| 83 | + _db.RunInTransaction(() => |
| 84 | + { |
| 85 | + _db.Update(dbCode); |
| 86 | + }); |
| 87 | + AllCodes[GetIndex(code)] = code; |
| 88 | + AllCodesPropertyChanged(); |
| 89 | + } |
| 90 | + } |
| 91 | + catch |
| 92 | + { |
| 93 | + return false; |
| 94 | + } |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + public bool DelCode(CodeBoxModel code) |
| 99 | + { |
| 100 | + try |
| 101 | + { |
| 102 | + _db.Delete<CodeTable>(code.id); |
| 103 | + AllCodes.Remove(code); |
| 104 | + AllCodesPropertyChanged(); |
| 105 | + } |
| 106 | + catch |
| 107 | + { |
| 108 | + return false; |
| 109 | + } |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + private List<string> AllOrderKind = new List<string>() |
| 114 | + { |
| 115 | + "Alphabet", "Date", "All languages", "Python", "C++", "C#", "CSS", "Dart", "Golang", |
| 116 | + "Html", "Java", "Javascript", "Kotlin", "Php", "C", "Ruby", "Rust","Sql", "Swift" |
| 117 | + }; |
| 118 | + public void OrderCode(string order) |
| 119 | + { |
| 120 | + int ind = AllOrderKind.IndexOf(order); |
| 121 | + ObservableCollection<CodeBoxModel> lst; |
| 122 | + |
| 123 | + if (ind > 2) |
| 124 | + { |
| 125 | + lst = new ObservableCollection<CodeBoxModel>(); |
| 126 | + foreach (var code in AllCodes) if (code.langType == order) lst.Add(code); |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + if (ind == 0) lst = new ObservableCollection<CodeBoxModel>(AllCodes.OrderBy(x => x.title).ToList()); |
| 131 | + else if(ind == 1) lst = new ObservableCollection<CodeBoxModel>(AllCodes.OrderBy(x => x.timestamp).ToList()); |
| 132 | + else |
| 133 | + { |
| 134 | + var codes = _db.Query<CodeTable>("SELECT * From CodeTable"); |
| 135 | + lst = new ObservableCollection<CodeBoxModel>(); |
| 136 | + foreach(var c in codes) |
| 137 | + { |
| 138 | + lst.Add(new CodeBoxModel(c.Id, c.title, c.desc, c.isFav, c.lang, c.code, c.timestamp, themeService)); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + AllCodes = lst; |
| 143 | + AllCodesPropertyChanged(); |
| 144 | + } |
| 145 | + |
| 146 | + public bool FavModify(CodeBoxModel code) |
| 147 | + { |
| 148 | + try |
| 149 | + { |
| 150 | + var dbCode = _db.Query<CodeTable>("select * from CodeTable where Id = ?", code.id).FirstOrDefault(); |
| 151 | + if (dbCode is not null) |
| 152 | + { |
| 153 | + dbCode.isFav = code.isFav; |
| 154 | + _db.RunInTransaction(() => |
| 155 | + { |
| 156 | + _db.Update(dbCode); |
| 157 | + }); |
| 158 | + AllCodes[GetIndex(code)] = code; |
| 159 | + AllCodesPropertyChanged(); |
| 160 | + } |
| 161 | + } |
| 162 | + catch |
| 163 | + { |
| 164 | + return false; |
| 165 | + } |
| 166 | + return true; |
| 167 | + } |
| 168 | + |
| 169 | + public void SearchCode(string text) |
| 170 | + { |
| 171 | + |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments