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

ArticlesDataAccess.cs 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. public static IList<string> Types = new List<string>{"home", "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. public static 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. var q = string.IsNullOrEmpty(filter.Query) ? null : filter.Query;
  48. return articles => (type == null || articles.type == type) &&
  49. (q == null || (WsDbContext.lu_texts_match(q, articles.text) ||
  50. WsDbContext.lu_texts_match(q, articles.id)));
  51. }
  52. protected override articles GetModelFromTCreate(ArticlesAddDbo obj)
  53. {
  54. return new articles
  55. {
  56. id = obj.Id,
  57. picture_caption = obj.PictureCaption,
  58. picture_url = obj.PictureUrl,
  59. type = obj.Type,
  60. articles_fields_fk = obj.Fields.Select(dbo => new articles_fields
  61. {
  62. property = dbo.Property,
  63. type = dbo.Type,
  64. value = dbo.Value
  65. }).ToList(),
  66. text = obj.Text
  67. };
  68. }
  69. protected override void EditModelFromTUpdate(ArticlesEditDbo obj, articles model)
  70. {
  71. model.picture_caption = obj.PictureCaption;
  72. model.picture_url = obj.PictureUrl;
  73. model.text = obj.Text;
  74. }
  75. protected override LuResult<bool> _EditSingleById(articles model, ArticlesEditDbo update, WsDbContext db, IQueryable<articles> table)
  76. {
  77. foreach (var removed in model.articles_fields_fk.Where(fields => update.Fields.FirstOrDefault(dbo => dbo.Property == fields.property) == null).ToList())
  78. {
  79. model.articles_fields_fk.Remove(removed);
  80. }
  81. foreach (var field in model.articles_fields_fk)
  82. {
  83. var f = update.Fields.First(dbo => dbo.Property == field.property);
  84. field.value = f.Value;
  85. field.property = f.Property;
  86. field.type = f.Type;
  87. }
  88. foreach (var added in update.Fields.Where(dbo => model.articles_fields_fk.FirstOrDefault(fields => dbo.Property == fields.property) == null))
  89. {
  90. model.articles_fields_fk.Add(new articles_fields
  91. {
  92. value = added.Value,
  93. property = added.Property,
  94. type = added.Type,
  95. article_id = model.id
  96. });
  97. }
  98. return LuResult<bool>.Ok(true);
  99. }
  100. protected override ArticlesDbo GetDboFromModel(articles model)
  101. {
  102. return model.ToDbo();
  103. }
  104. }
  105. }