| | 1 | | using Microsoft.Extensions.Logging; |
| | 2 | | using MRA.Infrastructure.Settings; |
| | 3 | | using MRA.DTO.ViewModels.Art; |
| | 4 | | using MRA.Infrastructure.Enums; |
| | 5 | | using MRA.Services.Models.Inspirations; |
| | 6 | | using MRA.Services.Models.Collections; |
| | 7 | | using MRA.DTO.Models; |
| | 8 | | using MRA.Services.Models.Drawings; |
| | 9 | | using MRA.Services.RemoteConfig; |
| | 10 | | using MRA.Services.Cache; |
| | 11 | | using MRA.Services.Storage; |
| | 12 | | using MRA.DTO.Enums.Drawing; |
| | 13 | | using MRA.Infrastructure.Cache; |
| | 14 | | using MRA.DTO.Enums.DrawingFilter; |
| | 15 | |
|
| | 16 | | namespace MRA.Services |
| | 17 | | { |
| | 18 | | public class AppService : CacheServiceBase, IAppService |
| | 19 | | { |
| | 20 | | private readonly IStorageService _storageService; |
| | 21 | | private readonly IDrawingService _drawingService; |
| | 22 | | private readonly IInspirationService _inspirationService; |
| | 23 | | private readonly ICollectionService _collectionService; |
| | 24 | | private readonly ILogger<AppService> _logger; |
| | 25 | | private readonly IRemoteConfigService _remoteConfigService; |
| | 26 | |
|
| | 27 | | public const string CACHE_ALL_DRAWINGS = "all_drawings"; |
| | 28 | | public const string CACHE_ALL_INSPIRATIONS = "all_inspirations"; |
| | 29 | | public const string CACHE_ALL_COLLECTIONS = "all_collections"; |
| | 30 | |
|
| | 31 | | public AppService( |
| | 32 | | ICacheProvider cache, |
| | 33 | | IStorageService storageService, |
| | 34 | | IRemoteConfigService remoteConfigService, |
| | 35 | | IDrawingService drawingService, |
| | 36 | | IInspirationService inspirationService, |
| | 37 | | ICollectionService collectionService, |
| | 38 | | ILogger<AppService> logger, |
| 35 | 39 | | AppSettings appConfig) : base(appConfig, cache) |
| | 40 | | { |
| 35 | 41 | | _logger = logger; |
| 35 | 42 | | _storageService = storageService; |
| 35 | 43 | | _remoteConfigService = remoteConfigService; |
| 35 | 44 | | _drawingService = drawingService; |
| 35 | 45 | | _inspirationService = inspirationService; |
| 35 | 46 | | _collectionService = collectionService; |
| 35 | 47 | | } |
| | 48 | |
|
| | 49 | | public async Task<IEnumerable<DrawingModel>> GetAllDrawings(bool onlyIfVisible, bool cache = true) |
| | 50 | | { |
| 3 | 51 | | return await GetOrSetFromCacheAsync(CACHE_ALL_DRAWINGS + onlyIfVisible, async () => |
| 3 | 52 | | { |
| 3 | 53 | | return await _drawingService.GetAllDrawingsAsync(onlyIfVisible); |
| 3 | 54 | | }, |
| 3 | 55 | | useCache: cache); |
| 3 | 56 | | } |
| | 57 | |
|
| | 58 | | public async Task<IEnumerable<InspirationModel>> GetAllInspirations() |
| | 59 | | { |
| 0 | 60 | | return await GetOrSetFromCacheAsync(CACHE_ALL_INSPIRATIONS, async () => |
| 0 | 61 | | { |
| 0 | 62 | | return await _inspirationService.GetAllInspirationsAsync(); |
| 0 | 63 | | }, |
| 0 | 64 | | useCache: true); |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | public async Task<IEnumerable<CollectionModel>> GetAllCollectionsAsync(bool onlyIfVisible, bool cache = true) |
| | 68 | | { |
| 0 | 69 | | return await GetOrSetFromCacheAsync(CACHE_ALL_COLLECTIONS + onlyIfVisible, async () => |
| 0 | 70 | | { |
| 0 | 71 | | return await FetchCollectionsAndLinkDrawings(onlyIfVisible, cache); |
| 0 | 72 | | }, |
| 0 | 73 | | useCache: cache); |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | private async Task<IEnumerable<CollectionModel>> FetchCollectionsAndLinkDrawings(bool onlyIfVisible, bool cache) |
| | 77 | | { |
| 0 | 78 | | var collections = await _collectionService.GetAllCollectionsAsync(onlyIfVisible); |
| 0 | 79 | | var drawgins = await GetAllDrawings(onlyIfVisible, cache); |
| 0 | 80 | | return LinkDrawingsToCollections(collections, drawgins); |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | private async Task<CollectionModel> FetchCollectionAndLinkDrawings(string id, bool onlyIfVisible, bool cache) |
| | 84 | | { |
| 0 | 85 | | var collection = await _collectionService.FindCollectionAsync(id); |
| 0 | 86 | | var drawgins = await GetAllDrawings(onlyIfVisible, cache); |
| 0 | 87 | | return LinkDrawingToCollection(collection, drawgins); |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | private static IEnumerable<CollectionModel> LinkDrawingsToCollections(IEnumerable<CollectionModel> collections, |
| | 91 | | { |
| 31 | 92 | | var newCollections = new List<CollectionModel>(); |
| | 93 | |
|
| 64 | 94 | | foreach (var collection in collections) |
| 1 | 95 | | newCollections.Add(LinkDrawingToCollection(collection, drawings)); |
| | 96 | |
|
| 32 | 97 | | newCollections = newCollections.Where(c => c.Drawings.Any()).ToList(); |
| 31 | 98 | | return newCollections; |
| | 99 | | } |
| | 100 | |
|
| | 101 | | private static CollectionModel LinkDrawingToCollection(CollectionModel collection, IEnumerable<DrawingModel> dra |
| | 102 | | { |
| 4 | 103 | | collection.Drawings = drawgins.Where(d => (collection.DrawingIds.Contains(d.Id))); |
| 1 | 104 | | return collection; |
| | 105 | | } |
| | 106 | |
|
| | 107 | | public async Task<CollectionModel> FindCollectionByIdAsync(string collectionId, bool onlyIfVisible, bool cache = |
| | 108 | | { |
| 0 | 109 | | return await GetOrSetFromCacheAsync($"collection_{collectionId}", async () => |
| 0 | 110 | | { |
| 0 | 111 | | return await FetchCollectionAndLinkDrawings(collectionId, onlyIfVisible: onlyIfVisible, cache: cache); |
| 0 | 112 | | }, |
| 0 | 113 | | useCache: cache); |
| 0 | 114 | | } |
| | 115 | |
|
| | 116 | |
|
| | 117 | | public async Task<FilterResults> FilterDrawingsAsync(DrawingFilter filter) |
| | 118 | | { |
| 31 | 119 | | return await GetOrSetFromCacheAsync(filter.CacheKey, async () => |
| 31 | 120 | | { |
| 31 | 121 | | return await FilterGivenList(filter); |
| 31 | 122 | | }, |
| 31 | 123 | | useCache: true); |
| 31 | 124 | | } |
| | 125 | |
|
| | 126 | | private async Task<FilterResults> FilterGivenList(DrawingFilter filter) |
| | 127 | | { |
| 31 | 128 | | var drawings = await _drawingService.GetAllDrawingsAsync(filter.OnlyVisible); |
| 31 | 129 | | var collections = await _collectionService.GetAllCollectionsAsync(filter.OnlyVisible); |
| 31 | 130 | | collections = LinkDrawingsToCollections(collections, drawings); |
| | 131 | |
|
| 31 | 132 | | drawings = FilterDrawings(filter, drawings, collections); |
| | 133 | |
|
| 31 | 134 | | if (filter.OnlyFilterCollection()) |
| | 135 | | { |
| 2 | 136 | | var selectedCollection = collections.FirstOrDefault(x => x.Id == filter.Collection); |
| 1 | 137 | | if (selectedCollection != null) |
| | 138 | | { |
| 1 | 139 | | var list = new List<DrawingModel>(); |
| 5 | 140 | | foreach (var id in selectedCollection.Drawings.Select(x => x.Id)) |
| | 141 | | { |
| 2 | 142 | | var d = drawings.FirstOrDefault(x => x.Id == id); |
| 1 | 143 | | if (d != null) |
| | 144 | | { |
| 1 | 145 | | list.Add(d); |
| | 146 | | } |
| | 147 | | } |
| 1 | 148 | | drawings = list; |
| | 149 | | } |
| | 150 | | } |
| | 151 | | else |
| | 152 | | { |
| 30 | 153 | | drawings = SortDrawingsByFilter(filter, drawings); |
| | 154 | | } |
| 31 | 155 | | SetBlobUrl(ref drawings); |
| | 156 | |
|
| 31 | 157 | | return new FilterResults(drawings, collections, filter); |
| 31 | 158 | | } |
| | 159 | |
|
| | 160 | | private IEnumerable<DrawingModel> SortDrawingsByFilter(DrawingFilter filter, IEnumerable<DrawingModel> drawings) |
| | 161 | | { |
| 30 | 162 | | return filter.Sortby switch |
| 30 | 163 | | { |
| 1 | 164 | | DrawingFilterSortBy.Latest => drawings.SortByLatest(), |
| 1 | 165 | | DrawingFilterSortBy.Oldest => drawings.SortByOldest(), |
| 1 | 166 | | DrawingFilterSortBy.NameAZ => drawings.SortByNameAZ(), |
| 1 | 167 | | DrawingFilterSortBy.NameZA => drawings.SortByNameZA(), |
| 1 | 168 | | DrawingFilterSortBy.LikeAscending => drawings.SortByLikeAscending(), |
| 1 | 169 | | DrawingFilterSortBy.LikeDescending => drawings.SortByLikeDescending(), |
| 1 | 170 | | DrawingFilterSortBy.ViewsAscending => drawings.SortByViewsAscending(), |
| 1 | 171 | | DrawingFilterSortBy.ViewsDescending => drawings.SortByViewsDescending(), |
| 1 | 172 | | DrawingFilterSortBy.AuthorScoreAscending => drawings.SortByAuthorScoreAscending(), |
| 1 | 173 | | DrawingFilterSortBy.AuthorScoreDescending => drawings.SortByAuthorScoreDescending(), |
| 1 | 174 | | DrawingFilterSortBy.UserScoreAscending => drawings.SortByUserScoreAscending(), |
| 1 | 175 | | DrawingFilterSortBy.UserScoreDescending => drawings.SortByUserScoreDescending(), |
| 1 | 176 | | DrawingFilterSortBy.Fastest => drawings.SortByFastest(), |
| 1 | 177 | | DrawingFilterSortBy.Slowest => drawings.SortBySlowest(), |
| 16 | 178 | | _ => CalculatePopularityOfListDrawings(drawings).SortByPopularity() |
| 30 | 179 | | }; |
| | 180 | | } |
| | 181 | |
|
| | 182 | | private IEnumerable<DrawingModel> FilterDrawings(DrawingFilter filter, IEnumerable<DrawingModel> drawings, IEnum |
| | 183 | | { |
| 31 | 184 | | drawings = FilterDrawings_OnlyVisible(filter, drawings); |
| 31 | 185 | | drawings = FilterDrawings_OnlyFavorites(filter, drawings); |
| 31 | 186 | | drawings = FilterDrawings_TextQuery(filter, drawings); |
| 31 | 187 | | drawings = FilterDrawings_ProductName(filter, drawings); |
| 31 | 188 | | drawings = FilterDrawings_CharacterName(filter, drawings); |
| 31 | 189 | | drawings = FilterDrawings_ModelName(filter, drawings); |
| 31 | 190 | | drawings = FilterDrawings_Type(filter, drawings); |
| 31 | 191 | | drawings = FilterDrawings_ProductType(filter, drawings); |
| 31 | 192 | | drawings = FilterDrawings_Software(filter, drawings); |
| 31 | 193 | | drawings = FilterDrawings_Paper(filter, drawings); |
| 31 | 194 | | drawings = FilterDrawings_Spotify(filter, drawings); |
| 31 | 195 | | drawings = FilterDrawings_Collection(filter, drawings, collections); |
| 31 | 196 | | return drawings; |
| | 197 | | } |
| | 198 | |
|
| | 199 | | private static IEnumerable<DrawingModel> FilterDrawings_Collection(DrawingFilter filter, IEnumerable<DrawingMode |
| | 200 | | { |
| 31 | 201 | | if (!String.IsNullOrEmpty(filter.Collection)) |
| | 202 | | { |
| 2 | 203 | | var collection = collections.FirstOrDefault(x => x.Id.Equals(filter.Collection)); |
| | 204 | |
|
| 1 | 205 | | if (collection != null) |
| | 206 | | { |
| 2 | 207 | | var idsCollection = collection.Drawings.Select(x => x.Id).ToList(); |
| 2 | 208 | | drawings = drawings.Where(d => idsCollection.Contains(d.Id)).ToList(); |
| | 209 | | } |
| | 210 | | } |
| | 211 | |
|
| 31 | 212 | | return drawings; |
| | 213 | | } |
| | 214 | |
|
| | 215 | | private static IEnumerable<DrawingModel> FilterDrawings_Spotify(DrawingFilter filter, IEnumerable<DrawingModel> |
| | 216 | | { |
| 31 | 217 | | if (filter.Spotify != null) |
| | 218 | | { |
| 2 | 219 | | if (filter.Spotify ?? false) |
| | 220 | | { |
| 2 | 221 | | drawings = drawings.Where(x => !string.IsNullOrEmpty(x.SpotifyUrl)).ToList(); |
| | 222 | | } |
| | 223 | | else |
| | 224 | | { |
| 2 | 225 | | drawings = drawings.Where(x => string.IsNullOrEmpty(x.SpotifyUrl)).ToList(); |
| | 226 | | } |
| | 227 | | } |
| | 228 | |
|
| 31 | 229 | | return drawings; |
| | 230 | | } |
| | 231 | |
|
| | 232 | | private static IEnumerable<DrawingModel> FilterDrawings_Paper(DrawingFilter filter, IEnumerable<DrawingModel> dr |
| | 233 | | { |
| 31 | 234 | | if (filter.Paper != EnumExtensions.GetDefaultValue<DrawingPaperSizes>()) |
| | 235 | | { |
| 2 | 236 | | drawings = drawings.Where(x => x.Paper == (int) filter.Paper).ToList(); |
| | 237 | | } |
| | 238 | |
|
| 31 | 239 | | return drawings; |
| | 240 | | } |
| | 241 | |
|
| | 242 | | private static IEnumerable<DrawingModel> FilterDrawings_Software(DrawingFilter filter, IEnumerable<DrawingModel> |
| | 243 | | { |
| 31 | 244 | | if (filter.Software != EnumExtensions.GetDefaultValue<DrawingSoftwares>()) |
| | 245 | | { |
| 2 | 246 | | drawings = drawings.Where(x => x.Software == (int) filter.Software).ToList(); |
| | 247 | | } |
| | 248 | |
|
| 31 | 249 | | return drawings; |
| | 250 | | } |
| | 251 | |
|
| | 252 | | private static IEnumerable<DrawingModel> FilterDrawings_ProductType(DrawingFilter filter, IEnumerable<DrawingMod |
| | 253 | | { |
| 31 | 254 | | if (filter.ProductType != EnumExtensions.GetDefaultValue<DrawingProductTypes>()) |
| | 255 | | { |
| 2 | 256 | | drawings = drawings.Where(x => x.ProductType == (int) filter.ProductType).ToList(); |
| | 257 | | } |
| | 258 | |
|
| 31 | 259 | | return drawings; |
| | 260 | | } |
| | 261 | |
|
| | 262 | | private static IEnumerable<DrawingModel> FilterDrawings_Type(DrawingFilter filter, IEnumerable<DrawingModel> dra |
| | 263 | | { |
| 31 | 264 | | if (filter.Type != EnumExtensions.GetDefaultValue<DrawingTypes>()) |
| | 265 | | { |
| 2 | 266 | | drawings = drawings.Where(x => x.Type == (int) filter.Type).ToList(); |
| | 267 | | } |
| | 268 | |
|
| 31 | 269 | | return drawings; |
| | 270 | | } |
| | 271 | |
|
| | 272 | | private static IEnumerable<DrawingModel> FilterDrawings_ModelName(DrawingFilter filter, IEnumerable<DrawingModel |
| | 273 | | { |
| 31 | 274 | | if (!String.IsNullOrEmpty(filter.ModelName)) |
| | 275 | | { |
| 2 | 276 | | if (filter.ModelName.Equals(DrawingFilter.MODEL_NONE)) |
| | 277 | | { |
| 1 | 278 | | drawings = drawings.Where(x => |
| 2 | 279 | | string.IsNullOrEmpty(x.ModelName)).ToList(); |
| | 280 | | } |
| | 281 | | else |
| | 282 | | { |
| 1 | 283 | | drawings = drawings.Where(x => |
| 1 | 284 | | x.ModelName.Contains(filter.ModelName)).ToList(); |
| | 285 | |
|
| | 286 | | } |
| | 287 | | } |
| | 288 | |
|
| 31 | 289 | | return drawings; |
| | 290 | | } |
| | 291 | |
|
| | 292 | | private static IEnumerable<DrawingModel> FilterDrawings_CharacterName(DrawingFilter filter, IEnumerable<DrawingM |
| | 293 | | { |
| 31 | 294 | | if (!String.IsNullOrEmpty(filter.CharacterName)) |
| | 295 | | { |
| 2 | 296 | | if (filter.CharacterName.Equals(DrawingFilter.CHARACTER_NONE)) |
| | 297 | | { |
| 1 | 298 | | drawings = drawings.Where(x => |
| 2 | 299 | | string.IsNullOrEmpty(x.Name)).ToList(); |
| | 300 | | } |
| | 301 | | else |
| | 302 | | { |
| 1 | 303 | | drawings = drawings.Where(x => |
| 1 | 304 | | x.Name.Contains(filter.CharacterName)).ToList(); |
| | 305 | |
|
| | 306 | | } |
| | 307 | | } |
| | 308 | |
|
| 31 | 309 | | return drawings; |
| | 310 | | } |
| | 311 | |
|
| | 312 | | private static IEnumerable<DrawingModel> FilterDrawings_ProductName(DrawingFilter filter, IEnumerable<DrawingMod |
| | 313 | | { |
| 31 | 314 | | if (!String.IsNullOrEmpty(filter.ProductName)) |
| | 315 | | { |
| 2 | 316 | | if (filter.ProductName.Equals(DrawingFilter.PRODUCT_NONE)) |
| | 317 | | { |
| 1 | 318 | | drawings = drawings.Where(x => |
| 2 | 319 | | string.IsNullOrEmpty(x.ProductName)).ToList(); |
| | 320 | | } |
| | 321 | | else |
| | 322 | | { |
| 1 | 323 | | drawings = drawings.Where(x => |
| 2 | 324 | | x.ProductName.Contains(filter.ProductName)).ToList(); |
| | 325 | |
|
| | 326 | | } |
| | 327 | | } |
| | 328 | |
|
| 31 | 329 | | return drawings; |
| | 330 | | } |
| | 331 | |
|
| | 332 | | private IEnumerable<DrawingModel> FilterDrawings_TextQuery(DrawingFilter filter, IEnumerable<DrawingModel> drawi |
| | 333 | | { |
| 31 | 334 | | if (!String.IsNullOrEmpty(filter.TextQuery)) |
| | 335 | | { |
| 2 | 336 | | var tags = _drawingService.DeleteAndAdjustTags(filter.Tags).Select(x => x.ToLower()); |
| 1 | 337 | | drawings = drawings.Where(x => |
| 5 | 338 | | x.Tags.Join(tags, t1 => t1.ToLower(), t2 => t2, (t1, t2) => t1.Contains(t2)).Any()); |
| | 339 | | } |
| | 340 | |
|
| 31 | 341 | | return drawings; |
| | 342 | | } |
| | 343 | |
|
| | 344 | | private static IEnumerable<DrawingModel> FilterDrawings_OnlyFavorites(DrawingFilter filter, IEnumerable<DrawingM |
| | 345 | | { |
| 31 | 346 | | if (filter.Favorites) |
| | 347 | | { |
| 2 | 348 | | drawings = drawings.Where(x => x.Favorite).ToList(); |
| | 349 | | } |
| | 350 | |
|
| 31 | 351 | | return drawings; |
| | 352 | | } |
| | 353 | |
|
| | 354 | | private static IEnumerable<DrawingModel> FilterDrawings_OnlyVisible(DrawingFilter filter, IEnumerable<DrawingMod |
| | 355 | | { |
| 31 | 356 | | if (filter.OnlyVisible) |
| | 357 | | { |
| 2 | 358 | | drawings = drawings.Where(x => x.Visible).ToList(); |
| | 359 | | } |
| | 360 | |
|
| 31 | 361 | | return drawings; |
| | 362 | | } |
| | 363 | |
|
| | 364 | | private void SetBlobUrl(ref IEnumerable<DrawingModel> drawings) |
| | 365 | | { |
| 192 | 366 | | foreach (var d in drawings) |
| | 367 | | { |
| 65 | 368 | | d.UrlBase = _storageService.GetBlobURL(); |
| | 369 | | } |
| 31 | 370 | | } |
| | 371 | |
|
| | 372 | | public IEnumerable<DrawingModel> CalculatePopularityOfListDrawings(IEnumerable<DrawingModel> drawings) |
| | 373 | | { |
| 16 | 374 | | double wDate = _remoteConfigService.GetPopularityDate(); |
| 16 | 375 | | int wMonths = _remoteConfigService.GetPopularityMonths(); |
| 16 | 376 | | double wCritic = _remoteConfigService.GetPopularityCritic(); |
| 16 | 377 | | double wPopular = _remoteConfigService.GetPopularityPopular(); |
| 16 | 378 | | double wFavorite = _remoteConfigService.GetPopularityFavorite(); |
| | 379 | |
|
| 16 | 380 | | var newList = new List<DrawingModel>(); |
| 64 | 381 | | foreach (var d in drawings) |
| | 382 | | { |
| 16 | 383 | | d.CalculatePopularity(wDate, wMonths, wCritic, wPopular, wFavorite); |
| 16 | 384 | | newList.Add(d); |
| | 385 | | } |
| | 386 | |
|
| 16 | 387 | | return newList; |
| | 388 | | } |
| | 389 | |
|
| | 390 | |
|
| | 391 | | public async Task<DrawingModel> FindDrawingByIdAsync(string documentId, bool onlyIfVisible, bool updateViews = f |
| | 392 | | { |
| 0 | 393 | | return await GetOrSetFromCacheAsync($"drawing_{documentId}", async () => |
| 0 | 394 | | { |
| 0 | 395 | | return await _drawingService.FindDrawingAsync(documentId, onlyIfVisible, updateViews); |
| 0 | 396 | | }, |
| 0 | 397 | | useCache: cache); |
| 0 | 398 | | } |
| | 399 | | } |
| | 400 | | } |