ShopPlatform.Web 1.0.0.0
농산물, 소프트웨어 라이선스와 개발 용역을 직접 판매하는 CodeMaru 직영 쇼핑몰입니다.
로딩중...
검색중...
일치하는것 없음
ShopCustomerProfileStore.cs
이 파일의 문서화 페이지로 가기
1using System.Text.Json;
3
5
14public sealed class ShopCustomerProfileStore
15{
24 private readonly string _root;
33 private static readonly JsonSerializerOptions JsonOptions = new() { WriteIndented = true };
34
52 {
53 _root = options.ResolvedDataPath;
54 }
55
88 public async Task<ShopCustomerProfile?> GetAsync(string slug, string userId)
89 {
90 var profiles = await LoadAsync(slug).ConfigureAwait(false);
91 return profiles.FirstOrDefault(x => string.Equals(x.UserId, userId, StringComparison.Ordinal));
92 }
93
126 public async Task SaveAsync(string slug, ShopCustomerProfile profile)
127 {
128 var profiles = await LoadAsync(slug).ConfigureAwait(false);
129 var index = profiles.FindIndex(x => string.Equals(x.UserId, profile.UserId, StringComparison.Ordinal));
130 profile.UpdatedAt = DateTime.UtcNow;
131
132 if (index >= 0)
133 {
134 profiles[index] = profile;
135 }
136 else
137 {
138 profiles.Add(profile);
139 }
140
141 Directory.CreateDirectory(ShopDir(slug));
142 await File.WriteAllTextAsync(ProfilePath(slug), JsonSerializer.Serialize(profiles, JsonOptions)).ConfigureAwait(false);
143 }
144
169 private async Task<List<ShopCustomerProfile>> LoadAsync(string slug)
170 {
171 var path = ProfilePath(slug);
172 if (!File.Exists(path))
173 {
174 return [];
175 }
176
177 await using var stream = File.OpenRead(path);
178 return await JsonSerializer.DeserializeAsync<List<ShopCustomerProfile>>(stream, JsonOptions).ConfigureAwait(false) ?? [];
179 }
180
205 private string ShopDir(string slug) => Path.Combine(_root, slug);
230 private string ProfilePath(string slug) => Path.Combine(ShopDir(slug), "customers.json");
231}
async Task SaveAsync(string slug, ShopCustomerProfile profile)
async Task< List< ShopCustomerProfile > > LoadAsync(string slug)
async Task< ShopCustomerProfile?> GetAsync(string slug, string userId)
static readonly JsonSerializerOptions JsonOptions