Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ArticlesDataAccess.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using Luticate2.Utils.DataAccess;
  6. using Luticate2.Utils.Dbo.Filter;
  7. using Luticate2.Utils.Dbo.Result;
  8. using Microsoft.EntityFrameworkCore;
  9. using WebApiWebSem.DataAccess.Models;
  10. using WebApiWebSem.Dbo.Articles;
  11. namespace WebApiWebSem.DataAccess
  12. {
  13. public class ArticlesDataAccess : LuEfCrudDataAccess<articles, ArticlesAddDbo, ArticlesDbo, ArticlesEditDbo, WsDbContext, string>
  14. {
  15. protected static IList<string> Types = new List<string>{"persons", "locations", "countries"};
  16. public ArticlesDataAccess(IServiceProvider serviceProvider) : base(serviceProvider)
  17. {
  18. }
  19. protected override object GetId(string id)
  20. {
  21. return id;
  22. }
  23. protected override IQueryable<articles> GetGetQueryable(WsDbContext db, IQueryable<articles> table)
  24. {
  25. return table.Include(articles => articles.articles_fields_fk);
  26. }
  27. protected override IQueryable<articles> GetEditQueryable(WsDbContext db, IQueryable<articles> table)
  28. {
  29. return GetGetQueryable(db, table);
  30. }
  31. protected override DbSet<articles> GetTable(WsDbContext db)
  32. {
  33. return db.articles;
  34. }
  35. protected string GetFilterType(LuFilterDbo filter)
  36. {
  37. var type = filter.GetFilterString("type", null);
  38. if (type == null || !Types.Contains(type.ToLower()))
  39. {
  40. return null;
  41. }
  42. return type.ToLower();
  43. }
  44. protected override Expression<Func<articles, bool>> GetFilterExpression(LuFilterDbo filter)
  45. {
  46. var type = GetFilterType(filter);
  47. return articles => type == null || articles.type == type;
  48. }
  49. protected override articles GetModelFromTCreate(ArticlesAddDbo obj)
  50. {
  51. return new articles
  52. {
  53. id = obj.Id,
  54. picture_caption = obj.PictureCaption,
  55. picture_url = obj.PictureUrl,
  56. type = obj.Type,
  57. articles_fields_fk = obj.Fields.Select(dbo => new articles_fields
  58. {
  59. property = dbo.Property,
  60. type = dbo.Type,
  61. value = dbo.Value
  62. }).ToList(),
  63. text = obj.Text
  64. };
  65. }
  66. protected override void EditModelFromTUpdate(ArticlesEditDbo obj, articles model)
  67. {
  68. model.picture_caption = obj.PictureCaption;
  69. model.picture_url = obj.PictureUrl;
  70. model.text = obj.Text;
  71. }
  72. protected override LuResult<bool> _EditSingleById(articles model, ArticlesEditDbo update, WsDbContext db, IQueryable<articles> table)
  73. {
  74. foreach (var removed in model.articles_fields_fk.Where(fields => update.Fields.FirstOrDefault(dbo => dbo.Property == fields.property) == null).ToList())
  75. {
  76. model.articles_fields_fk.Remove(removed);
  77. }
  78. foreach (var field in model.articles_fields_fk)
  79. {
  80. var f = update.Fields.First(dbo => dbo.Property == field.property);
  81. field.value = f.Value;
  82. field.property = f.Property;
  83. field.type = f.Type;
  84. }
  85. foreach (var added in update.Fields.Where(dbo => model.articles_fields_fk.FirstOrDefault(fields => dbo.Property == fields.property) == null))
  86. {
  87. model.articles_fields_fk.Add(new articles_fields
  88. {
  89. value = added.Value,
  90. property = added.Property,
  91. type = added.Type,
  92. article_id = model.id
  93. });
  94. }
  95. return LuResult<bool>.Ok(true);
  96. }
  97. protected override ArticlesDbo GetDboFromModel(articles model)
  98. {
  99. return model.ToDbo();
  100. }
  101. }
  102. }