You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArticlesDataAccess.cs 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 Microsoft.EntityFrameworkCore;
  8. using WebApiWebSem.DataAccess.Models;
  9. using WebApiWebSem.Dbo.Articles;
  10. namespace WebApiWebSem.DataAccess
  11. {
  12. public class ArticlesDataAccess : LuEfCrudDataAccess<articles, ArticlesAddDbo, ArticlesDbo, ArticlesEditDbo, WsDbContext, string>
  13. {
  14. protected static IList<string> Types = new List<string>{"persons", "locations", "countries"};
  15. public ArticlesDataAccess(IServiceProvider serviceProvider) : base(serviceProvider)
  16. {
  17. }
  18. protected override object GetId(string id)
  19. {
  20. return id;
  21. }
  22. protected override IQueryable<articles> GetGetQueryable(WsDbContext db, IQueryable<articles> table)
  23. {
  24. return table.Include(articles => articles.articles_fields_fk);
  25. }
  26. protected override DbSet<articles> GetTable(WsDbContext db)
  27. {
  28. return db.articles;
  29. }
  30. protected string GetFilterType(LuFilterDbo filter)
  31. {
  32. var type = filter.GetFilterString("type", null);
  33. if (type == null || !Types.Contains(type.ToLower()))
  34. {
  35. return null;
  36. }
  37. return type.ToLower();
  38. }
  39. protected override Expression<Func<articles, bool>> GetFilterExpression(LuFilterDbo filter)
  40. {
  41. var type = GetFilterType(filter);
  42. return articles => type == null || articles.type == type;
  43. }
  44. protected override articles GetModelFromTCreate(ArticlesAddDbo obj)
  45. {
  46. return new articles
  47. {
  48. id = obj.Id,
  49. picture_caption = obj.PictureCaption,
  50. picture_url = obj.PictureUrl,
  51. type = obj.Type,
  52. articles_fields_fk = obj.Fields.Select(dbo => new articles_fields
  53. {
  54. property = dbo.Property,
  55. type = dbo.Type,
  56. value = dbo.Value
  57. }).ToList(),
  58. text = obj.Text
  59. };
  60. }
  61. protected override void EditModelFromTUpdate(ArticlesEditDbo obj, articles model)
  62. {
  63. model.picture_caption = obj.PictureCaption;
  64. model.picture_url = obj.PictureUrl;
  65. model.articles_fields_fk = obj.Fields.Select(dbo => new articles_fields
  66. {
  67. value = dbo.Value,
  68. property = dbo.Property,
  69. type = dbo.Type
  70. }).ToList();
  71. model.text = obj.Text;
  72. }
  73. protected override ArticlesDbo GetDboFromModel(articles model)
  74. {
  75. return model.ToDbo();
  76. }
  77. }
  78. }