Portfolio.Web 1.0.0.0
.NET, Blazor, WPF와 서비스 운영 경험을 프로젝트·이력·기술 스택 단위로 보여주는 개발자 포트폴리오입니다.
로딩중...
검색중...
일치하는것 없음
JsonContactStore.cs
이 파일의 문서화 페이지로 가기
1using System.IO;
2using System.Text.Json;
4
6
16{
25 private readonly string _root;
34 private static readonly JsonSerializerOptions _json = new() { WriteIndented = true };
35
53
78 private string Dir(string slug) => Path.Combine(_root, slug, "contacts");
111 private string Path_(string slug, string id) => Path.Combine(Dir(slug), $"{id}.json");
112
137 public async Task<List<ContactMessage>> GetAllAsync(string slug)
138 {
139 var dir = Dir(slug);
140 if (!Directory.Exists(dir)) return [];
141 var list = new List<ContactMessage>();
142 foreach (var f in Directory.GetFiles(dir, "*.json"))
143 {
144 try
145 {
146 var json = await File.ReadAllTextAsync(f);
147 var msg = JsonSerializer.Deserialize<ContactMessage>(json);
148 if (msg != null) list.Add(msg);
149 }
150 catch { }
151 }
152 return [.. list.OrderByDescending(m => m.SentAt)];
153 }
154
187 public async Task SaveAsync(string slug, ContactMessage msg)
188 {
189 Directory.CreateDirectory(Dir(slug));
190 var json = JsonSerializer.Serialize(msg, _json);
191 await File.WriteAllTextAsync(Path_(slug, msg.Id), json);
192 }
193
226 public Task DeleteAsync(string slug, string msgId)
227 {
228 var path = Path_(slug, msgId);
229 if (File.Exists(path)) File.Delete(path);
230 return Task.CompletedTask;
231 }
232
265 public async Task MarkReadAsync(string slug, string msgId)
266 {
267 var path = Path_(slug, msgId);
268 if (!File.Exists(path)) return;
269 var json = await File.ReadAllTextAsync(path);
270 var msg = JsonSerializer.Deserialize<ContactMessage>(json);
271 if (msg is null) return;
272 msg.IsRead = true;
273 await File.WriteAllTextAsync(path, JsonSerializer.Serialize(msg, _json));
274 }
275}
async Task< List< ContactMessage > > GetAllAsync(string slug)
static readonly JsonSerializerOptions _json
Task DeleteAsync(string slug, string msgId)
async Task SaveAsync(string slug, ContactMessage msg)
string Path_(string slug, string id)
async Task MarkReadAsync(string slug, string msgId)