Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

LuUsersDataAccess.cs 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Linq;
  3. using Luticate2.Auth.DataAccess.Models;
  4. using Luticate2.Auth.Dbo.Users;
  5. using Luticate2.Utils.DataAccess;
  6. using Luticate2.Utils.Dbo.Result;
  7. using Luticate2.Utils.Utils;
  8. using Microsoft.EntityFrameworkCore;
  9. using Newtonsoft.Json;
  10. using Npgsql;
  11. namespace Luticate2.Auth.DataAccess
  12. {
  13. public class LuUsersDataAccess : LuEfCrudDataAccess<lu_users, LuUsersAddFullDbo, LuUsersFullDbo, LuUsersEditFullDbo, LuAuthDatabaseContext, string>
  14. {
  15. public LuUsersDataAccess(IServiceProvider serviceProvider) : base(serviceProvider)
  16. {
  17. }
  18. protected override DbSet<lu_users> GetTable(LuAuthDatabaseContext db)
  19. {
  20. return db.lu_users;
  21. }
  22. protected override lu_users GetModelFromTCreate(LuUsersAddFullDbo obj)
  23. {
  24. return new lu_users
  25. {
  26. authentication_source_id = obj.AuthenticationSourceId.ToGuid(),
  27. data = JsonConvert.SerializeObject(obj.Data),
  28. password = obj.Password,
  29. salt = obj.Salt,
  30. username = obj.Username
  31. };
  32. }
  33. protected override LuResult<T> HandleError<T>(Exception e)
  34. {
  35. if (e is DbUpdateException && e.InnerException is PostgresException)
  36. {
  37. var pge = (PostgresException) e.InnerException;
  38. if (pge.ConstraintName == "lu_users_authentication_source_id_username_key")
  39. {
  40. return LuResult<T>.Error(LuStatus.InputError, e, "Username already exists");
  41. }
  42. }
  43. return null;
  44. }
  45. protected override void EditModelFromTUpdate(LuUsersEditFullDbo obj, lu_users model)
  46. {
  47. throw new NotImplementedException();//TODO
  48. }
  49. protected override LuUsersFullDbo GetDboFromModel(lu_users model)
  50. {
  51. return model.ToDbo();
  52. }
  53. public LuResult<LuUsersFullDbo> FindByUsername(string username)
  54. {
  55. return Execute((context, set) =>
  56. {
  57. var user = set.FirstOrDefault(users => users.username.ToLower() == username.ToLower())?.ToDbo();
  58. if (user != null)
  59. {
  60. return LuResult<LuUsersFullDbo>.Ok(user);
  61. }
  62. return LuResult<LuUsersFullDbo>.Error(LuStatus.NotFound, $"username: {username}", "Username not found");
  63. });
  64. }
  65. }
  66. }