using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using Luticate2.Utils.DataAccess; using Luticate2.Utils.Dbo.Filter; using Microsoft.EntityFrameworkCore; using WebApiWebSem.DataAccess.Models; using WebApiWebSem.Dbo.Articles; namespace WebApiWebSem.DataAccess { public class ArticlesDataAccess : LuEfCrudDataAccess { protected static IList Types = new List{"persons", "locations", "countries"}; public ArticlesDataAccess(IServiceProvider serviceProvider) : base(serviceProvider) { } protected override object GetId(string id) { return id; } protected override IQueryable GetGetQueryable(WsDbContext db, IQueryable table) { return table.Include(articles => articles.articles_fields_fk); } protected override DbSet GetTable(WsDbContext db) { return db.articles; } protected string GetFilterType(LuFilterDbo filter) { var type = filter.GetFilterString("type", null); if (type == null || !Types.Contains(type.ToLower())) { return null; } return type.ToLower(); } protected override Expression> GetFilterExpression(LuFilterDbo filter) { var type = GetFilterType(filter); return articles => type == null || articles.type == type; } protected override articles GetModelFromTCreate(ArticlesAddDbo obj) { return new articles { id = obj.Id, picture_caption = obj.PictureCaption, picture_url = obj.PictureUrl, type = obj.Type, articles_fields_fk = obj.Fields.Select(dbo => new articles_fields { property = dbo.Property, type = dbo.Type, value = dbo.Value }).ToList(), text = obj.Text }; } protected override void EditModelFromTUpdate(ArticlesEditDbo obj, articles model) { model.picture_caption = obj.PictureCaption; model.picture_url = obj.PictureUrl; model.articles_fields_fk = obj.Fields.Select(dbo => new articles_fields { value = dbo.Value, property = dbo.Property, type = dbo.Type }).ToList(); model.text = obj.Text; } protected override ArticlesDbo GetDboFromModel(articles model) { return model.ToDbo(); } } }