#169 db + import operations

This commit is contained in:
Hamid Reza Mohammadi 2021-07-01 19:42:21 +04:30
parent ad45a49f57
commit adea29bc5a
8 changed files with 2861 additions and 15 deletions

View File

@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json; using Newtonsoft.Json;
using RMuseum.Models.Auth.Memory; using RMuseum.Models.Auth.Memory;
using RMuseum.Models.Auth.ViewModel; using RMuseum.Models.Auth.ViewModel;
@ -304,7 +305,7 @@ namespace RMuseum.Controllers
{ {
var poet = await _ganjoorService.GetPoetById(id); var poet = await _ganjoorService.GetPoetById(id);
IFormFile file = Request.Form.Files[0]; IFormFile file = Request.Form.Files[0];
RServiceResult<RImage> image = await _imageFileService.Add(file, null, file.FileName, $"C:\\PoetImages\\{poet.Result.Cat.UrlSlug}.png"); RServiceResult<RImage> image = await _imageFileService.Add(file, null, file.FileName, Path.Combine(_configuration.GetSection("PictureFileService")["StoragePath"], "PoetImages"));
if (!string.IsNullOrEmpty(image.ExceptionString)) if (!string.IsNullOrEmpty(image.ExceptionString))
{ {
return BadRequest(image.ExceptionString); return BadRequest(image.ExceptionString);
@ -1557,6 +1558,11 @@ namespace RMuseum.Controllers
/// </summary> /// </summary>
protected readonly IMemoryCache _memoryCache; protected readonly IMemoryCache _memoryCache;
/// <summary>
/// Configuration
/// </summary>
protected IConfiguration _configuration { get; }
/// <summary> /// <summary>
/// constructor /// constructor
/// </summary> /// </summary>
@ -1565,13 +1571,22 @@ namespace RMuseum.Controllers
/// <param name="httpContextAccessor"></param> /// <param name="httpContextAccessor"></param>
/// <param name="imageFileService"></param> /// <param name="imageFileService"></param>
/// <param name="memoryCache"></param> /// <param name="memoryCache"></param>
public GanjoorController(IGanjoorService ganjoorService, IAppUserService appUserService, IHttpContextAccessor httpContextAccessor, IImageFileService imageFileService, IMemoryCache memoryCache) /// <param name="configuration"></param>
public GanjoorController(
IGanjoorService ganjoorService,
IAppUserService appUserService,
IHttpContextAccessor httpContextAccessor,
IImageFileService imageFileService,
IMemoryCache memoryCache,
IConfiguration configuration
)
{ {
_ganjoorService = ganjoorService; _ganjoorService = ganjoorService;
_appUserService = appUserService; _appUserService = appUserService;
_httpContextAccessor = httpContextAccessor; _httpContextAccessor = httpContextAccessor;
_imageFileService = imageFileService; _imageFileService = imageFileService;
_memoryCache = memoryCache; _memoryCache = memoryCache;
_configuration = configuration;
} }
} }
} }

View File

@ -22,7 +22,7 @@ namespace RMuseum.DbContext
{ {
public RMuseumDbContext(DbContextOptions<RMuseumDbContext> options) : base(options) public RMuseumDbContext(DbContextOptions<RMuseumDbContext> options) : base(options)
{ {
//Database.Migrate(); Database.Migrate();
} }
/// <summary> /// <summary>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,45 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
namespace RMuseum.Migrations
{
public partial class SingerImage : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<Guid>(
name: "RImageId",
table: "GanjoorSingers",
type: "uniqueidentifier",
nullable: true);
migrationBuilder.CreateIndex(
name: "IX_GanjoorSingers_RImageId",
table: "GanjoorSingers",
column: "RImageId");
migrationBuilder.AddForeignKey(
name: "FK_GanjoorSingers_GeneralImages_RImageId",
table: "GanjoorSingers",
column: "RImageId",
principalTable: "GeneralImages",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_GanjoorSingers_GeneralImages_RImageId",
table: "GanjoorSingers");
migrationBuilder.DropIndex(
name: "IX_GanjoorSingers_RImageId",
table: "GanjoorSingers");
migrationBuilder.DropColumn(
name: "RImageId",
table: "GanjoorSingers");
}
}
}

View File

@ -1314,6 +1314,9 @@ namespace RMuseum.Migrations
b.Property<string>("Name") b.Property<string>("Name")
.HasColumnType("nvarchar(450)"); .HasColumnType("nvarchar(450)");
b.Property<Guid?>("RImageId")
.HasColumnType("uniqueidentifier");
b.Property<string>("Url") b.Property<string>("Url")
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
@ -1321,6 +1324,8 @@ namespace RMuseum.Migrations
b.HasIndex("Name"); b.HasIndex("Name");
b.HasIndex("RImageId");
b.ToTable("GanjoorSingers"); b.ToTable("GanjoorSingers");
}); });
@ -2438,6 +2443,15 @@ namespace RMuseum.Migrations
b.Navigation("Singer"); b.Navigation("Singer");
}); });
modelBuilder.Entity("RMuseum.Models.MusicCatalogue.GanjoorSinger", b =>
{
b.HasOne("RSecurityBackend.Models.Image.RImage", "RImage")
.WithMany()
.HasForeignKey("RImageId");
b.Navigation("RImage");
});
modelBuilder.Entity("RMuseum.Models.MusicCatalogue.GanjoorTrack", b => modelBuilder.Entity("RMuseum.Models.MusicCatalogue.GanjoorTrack", b =>
{ {
b.HasOne("RMuseum.Models.MusicCatalogue.GanjoorAlbum", "Album") b.HasOne("RMuseum.Models.MusicCatalogue.GanjoorAlbum", "Album")

View File

@ -1,4 +1,6 @@
using System.Collections.Generic; using RSecurityBackend.Models.Image;
using System;
using System.Collections.Generic;
namespace RMuseum.Models.MusicCatalogue namespace RMuseum.Models.MusicCatalogue
{ {
@ -21,10 +23,21 @@ namespace RMuseum.Models.MusicCatalogue
/// url /// url
/// </summary> /// </summary>
public string Url { get; set; } public string Url { get; set; }
/// <summary>
/// poet image
/// </summary>
public virtual RImage RImage { get; set; }
/// <summary>
/// user image id
/// </summary>
public Guid? RImageId { get; set; }
/// <summary> /// <summary>
/// albums /// albums
/// </summary> /// </summary>
public ICollection<GanjoorAlbum> Albums { get; set; } public ICollection<GanjoorAlbum> Albums { get; set; }
} }
} }

View File

@ -1145,7 +1145,12 @@
IMemoryCache IMemoryCache
</summary> </summary>
</member> </member>
<member name="M:RMuseum.Controllers.GanjoorController.#ctor(RMuseum.Services.IGanjoorService,RSecurityBackend.Services.IAppUserService,Microsoft.AspNetCore.Http.IHttpContextAccessor,RSecurityBackend.Services.IImageFileService,Microsoft.Extensions.Caching.Memory.IMemoryCache)"> <member name="P:RMuseum.Controllers.GanjoorController._configuration">
<summary>
Configuration
</summary>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.#ctor(RMuseum.Services.IGanjoorService,RSecurityBackend.Services.IAppUserService,Microsoft.AspNetCore.Http.IHttpContextAccessor,RSecurityBackend.Services.IImageFileService,Microsoft.Extensions.Caching.Memory.IMemoryCache,Microsoft.Extensions.Configuration.IConfiguration)">
<summary> <summary>
constructor constructor
</summary> </summary>
@ -1154,6 +1159,7 @@
<param name="httpContextAccessor"></param> <param name="httpContextAccessor"></param>
<param name="imageFileService"></param> <param name="imageFileService"></param>
<param name="memoryCache"></param> <param name="memoryCache"></param>
<param name="configuration"></param>
</member> </member>
<member name="M:RMuseum.Controllers.ImageController.Get(System.Guid,System.String)"> <member name="M:RMuseum.Controllers.ImageController.Get(System.Guid,System.String)">
<summary> <summary>
@ -5948,6 +5954,16 @@
url url
</summary> </summary>
</member> </member>
<member name="P:RMuseum.Models.MusicCatalogue.GanjoorSinger.RImage">
<summary>
poet image
</summary>
</member>
<member name="P:RMuseum.Models.MusicCatalogue.GanjoorSinger.RImageId">
<summary>
user image id
</summary>
</member>
<member name="P:RMuseum.Models.MusicCatalogue.GanjoorSinger.Albums"> <member name="P:RMuseum.Models.MusicCatalogue.GanjoorSinger.Albums">
<summary> <summary>
albums albums

View File

@ -1,13 +1,17 @@
using DNTPersianUtils.Core; using DNTPersianUtils.Core;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json; using Newtonsoft.Json;
using RMuseum.DbContext; using RMuseum.DbContext;
using RMuseum.Models.Ganjoor; using RMuseum.Models.Ganjoor;
using RMuseum.Models.MusicCatalogue;
using RSecurityBackend.Models.Generic; using RSecurityBackend.Models.Generic;
using RSecurityBackend.Models.Generic.Db; using RSecurityBackend.Models.Generic.Db;
using RSecurityBackend.Models.Image;
using RSecurityBackend.Services.Implementation; using RSecurityBackend.Services.Implementation;
using System; using System;
using System.Data; using System.Data;
using System.IO;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -19,15 +23,103 @@ namespace RMuseum.Services.Implementation
/// </summary> /// </summary>
public partial class GanjoorService : IGanjoorService public partial class GanjoorService : IGanjoorService
{ {
private async Task _PerformMundexHouseKeepingAndPreparation(RMuseumDbContext context, LongRunningJobProgressServiceEF jobProgressServiceEF, RLongRunningJobStatus job)
{
var singers = await context.GanjoorSingers.ToListAsync();
foreach(var singer in singers)
if(!string.IsNullOrEmpty(singer.Url) && singer.Url.Length > 0 && singer.Url[singer.Url.Length - 1] == '/')
{
singer.Url = singer.Url.Substring(0, singer.Url.Length - 1);
context.GanjoorSingers.Update(singer);
}
await context.SaveChangesAsync();
await jobProgressServiceEF.UpdateJob(job.Id, 2);
var poemMusicTracks =
await context.GanjoorPoemMusicTracks
.Where(m =>
m.TrackType == PoemMusicTrackType.BeepTunesOrKhosousi
&&
m.Approved)
.ToListAsync();
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json")
.Build();
ImageFileServiceEF imageFileService = new ImageFileServiceEF(context, configuration);
using (HttpClient httpClient = new HttpClient())
foreach (var poemMusicTrack in poemMusicTracks)
{
if (!string.IsNullOrEmpty(poemMusicTrack.ArtistUrl) && poemMusicTrack.ArtistUrl.Length > 0 && poemMusicTrack.ArtistUrl[poemMusicTrack.ArtistUrl.Length - 1] == '/')
{
poemMusicTrack.ArtistUrl = poemMusicTrack.ArtistUrl.Substring(0, poemMusicTrack.ArtistUrl.Length - 1);
context.GanjoorPoemMusicTracks.Update(poemMusicTrack);
}
var singer = context.GanjoorSingers.Where(s => s.Url == poemMusicTrack.ArtistUrl).FirstOrDefault();
if (singer == null)
{
singer = new GanjoorSinger()
{
Name = poemMusicTrack.ArtistName,
Url = poemMusicTrack.ArtistUrl
};
context.GanjoorSingers.Add(singer);
await context.SaveChangesAsync();
}
//singer image:
if (singer.Url.Contains("beeptunes.com/artist/"))
{
if (singer.RImageId == null)
{
var bUrl = singer.Url;
var beepId = bUrl.Substring(bUrl.LastIndexOf("/") + 1);
var response = await httpClient.GetAsync($"https://newapi.beeptunes.com/public/artist/info/?artistId={beepId}");
if (response.IsSuccessStatusCode)
{
dynamic bpArtist = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
if (bpArtist.artistImage != null)
{
var imageResult = await httpClient.GetAsync(bpArtist.artistImage.ToString());
if (imageResult.IsSuccessStatusCode)
{
using (Stream imageStream = await imageResult.Content.ReadAsStreamAsync())
{
RServiceResult<RImage> image = await imageFileService.Add(null, imageStream, $"{beepId}.jpg", Path.Combine(configuration.GetSection("PictureFileService")["StoragePath"], "SingerImages"));
if (string.IsNullOrEmpty(image.ExceptionString))
{
image = await imageFileService.Store(image.Result);
if (string.IsNullOrEmpty(image.ExceptionString))
{
singer.RImageId = image.Result.Id;
context.GanjoorSingers.Update(singer);
await context.SaveChangesAsync();
}
}
}
}
}
}
}
}
}
await context.SaveChangesAsync();
await jobProgressServiceEF.UpdateJob(job.Id, 3);
}
private async Task _UpdateMundexPage(Guid editingUserId, RMuseumDbContext context, LongRunningJobProgressServiceEF jobProgressServiceEF, RLongRunningJobStatus job) private async Task _UpdateMundexPage(Guid editingUserId, RMuseumDbContext context, LongRunningJobProgressServiceEF jobProgressServiceEF, RLongRunningJobStatus job)
{ {
var poemMusicTracks = var poemMusicTracks =
await context.GanjoorPoemMusicTracks await context.GanjoorPoemMusicTracks
.Where(m => .Where(m =>
m.TrackType == PoemMusicTrackType.BeepTunesOrKhosousi m.TrackType == PoemMusicTrackType.BeepTunesOrKhosousi
&& &&
m.Approved) m.Approved)
.ToListAsync(); .ToListAsync();
var dbPage = await context.GanjoorPages.Where(p => p.FullUrl == "/mundex").SingleAsync(); var dbPage = await context.GanjoorPages.Where(p => p.FullUrl == "/mundex").SingleAsync();
@ -181,13 +273,14 @@ namespace RMuseum.Services.Implementation
using (RMuseumDbContext context = new RMuseumDbContext(new DbContextOptions<RMuseumDbContext>())) //this is long running job, so _context might be already been freed/collected by GC using (RMuseumDbContext context = new RMuseumDbContext(new DbContextOptions<RMuseumDbContext>())) //this is long running job, so _context might be already been freed/collected by GC
{ {
LongRunningJobProgressServiceEF jobProgressServiceEF = new LongRunningJobProgressServiceEF(context); LongRunningJobProgressServiceEF jobProgressServiceEF = new LongRunningJobProgressServiceEF(context);
var job = (await jobProgressServiceEF.NewJob("UpdateMundexPage", "Mundex Page")).Result; var job = (await jobProgressServiceEF.NewJob("UpdateMundexPage", "House Keeping")).Result;
try try
{ {
await _PerformMundexHouseKeepingAndPreparation(context, jobProgressServiceEF, job);
await jobProgressServiceEF.UpdateJob(job.Id, 10, "Mundex Page");
await _UpdateMundexPage(editingUserId, context, jobProgressServiceEF, job); await _UpdateMundexPage(editingUserId, context, jobProgressServiceEF, job);
await jobProgressServiceEF.UpdateJob(job.Id, 50, "Mundex by poet page"); await jobProgressServiceEF.UpdateJob(job.Id, 60, "Mundex by poet page");
await _UpdateMundexByPoetPage(editingUserId, context, jobProgressServiceEF, job); await _UpdateMundexByPoetPage(editingUserId, context, jobProgressServiceEF, job);
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true); await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true);