12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- using System;
- using System.Linq;
- using Luticate2.Auth.DataAccess.Models;
- using Luticate2.Auth.Dbo.Users;
- using Luticate2.Utils.DataAccess;
- using Luticate2.Utils.Dbo.Result;
- using Luticate2.Utils.Utils;
- using Microsoft.EntityFrameworkCore;
- using Newtonsoft.Json;
- using Npgsql;
-
- namespace Luticate2.Auth.DataAccess
- {
- public class LuUsersDataAccess : LuEfCrudDataAccess<lu_users, LuUsersAddFullDbo, LuUsersFullDbo, LuUsersEditFullDbo, LuAuthDatabaseContext, string>
- {
- public LuUsersDataAccess(IServiceProvider serviceProvider) : base(serviceProvider)
- {
- }
-
- protected override DbSet<lu_users> GetTable(LuAuthDatabaseContext db)
- {
- return db.lu_users;
- }
-
- protected override lu_users GetModelFromTCreate(LuUsersAddFullDbo obj)
- {
- return new lu_users
- {
- authentication_source_id = obj.AuthenticationSourceId.ToGuid(),
- data = JsonConvert.SerializeObject(obj.Data),
- password = obj.Password,
- salt = obj.Salt,
- username = obj.Username
- };
- }
-
- protected override LuResult<T> HandleError<T>(Exception e)
- {
- if (e is DbUpdateException && e.InnerException is PostgresException)
- {
- var pge = (PostgresException) e.InnerException;
- if (pge.ConstraintName == "lu_users_authentication_source_id_username_key")
- {
- return LuResult<T>.Error(LuStatus.InputError, e, "Username already exists");
- }
- }
- return null;
- }
-
- protected override void EditModelFromTUpdate(LuUsersEditFullDbo obj, lu_users model)
- {
- throw new NotImplementedException();//TODO
- }
-
- protected override LuUsersFullDbo GetDboFromModel(lu_users model)
- {
- return model.ToDbo();
- }
-
- public LuResult<LuUsersFullDbo> FindByUsername(string username)
- {
- return Execute((context, set) =>
- {
- var user = set.FirstOrDefault(users => users.username.ToLower() == username.ToLower())?.ToDbo();
- if (user != null)
- {
- return LuResult<LuUsersFullDbo>.Ok(user);
- }
- return LuResult<LuUsersFullDbo>.Error(LuStatus.NotFound, $"username: {username}", "Username not found");
- });
- }
- }
- }
|