Changed phone number to use PhoneNumber.cs implicit string
This commit is contained in:
parent
1086396ccd
commit
e2140d83f2
@ -1,4 +1,5 @@
|
||||
using DAL.Models;
|
||||
using DAL.Values;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace API.DTO.Base.Update
|
||||
@ -11,7 +12,7 @@ namespace API.DTO.Base.Update
|
||||
[MaxLength(64)]
|
||||
public string? lastName { get; set; }
|
||||
|
||||
public ulong? phoneNumber { get; set; }
|
||||
public PhoneNumber? phoneNumber { get; set; }
|
||||
|
||||
public ulong? permissionId { get; set; }
|
||||
|
||||
@ -19,7 +20,7 @@ namespace API.DTO.Base.Update
|
||||
{
|
||||
if (firstName != null) model.firstName = firstName;
|
||||
if (lastName != null) model.lastName = lastName;
|
||||
if (phoneNumber != null) model.phoneNumber = (ulong)phoneNumber;
|
||||
if (phoneNumber != null) model.phoneNumber = phoneNumber;
|
||||
if (permissionId != null) model.permissionId = (ulong)permissionId;
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using DAL.Models;
|
||||
using DAL.Values;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace API.DTO.Base
|
||||
@ -13,7 +14,7 @@ namespace API.DTO.Base
|
||||
[MaxLength(64)]
|
||||
public string lastName { get; set; } = null!;
|
||||
|
||||
public ulong phoneNumber { get; set; }
|
||||
public PhoneNumber phoneNumber { get; set; } = null!;
|
||||
|
||||
public ulong permissionId { get; set; }
|
||||
|
||||
|
@ -1,10 +1,11 @@
|
||||
using DAL.Values;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace API.DTO.Login
|
||||
{
|
||||
public class UserLoginDTO
|
||||
{
|
||||
public ulong phoneNumber { get; set; }
|
||||
public PhoneNumber phoneNumber { get; set; } = null!;
|
||||
|
||||
[MaxLength(100)]
|
||||
public string password { get; set; } = null!;
|
||||
|
@ -23,7 +23,7 @@ namespace API.Services
|
||||
|
||||
public UserDTO? AuthenticateUser(UserLoginDTO loginDTO)
|
||||
{
|
||||
User? user = _userService.getNoAuthentication(x => x.phoneNumber == loginDTO.phoneNumber).FirstOrDefault();
|
||||
User? user = _userService.getNoAuthentication(x => x.phoneNumber.Equals(loginDTO.phoneNumber)).FirstOrDefault();
|
||||
|
||||
if (user == null)
|
||||
return null;
|
||||
|
@ -1,5 +1,7 @@
|
||||
using DAL.Converters;
|
||||
using DAL.Models;
|
||||
using DAL.Models.Audits;
|
||||
using DAL.Values;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DAL.Contexts
|
||||
@ -31,6 +33,11 @@ namespace DAL.Contexts
|
||||
public virtual DbSet<AuditSavedEvent> auditSavedEvents { get; set; }
|
||||
public virtual DbSet<AuditUser> auditUsers { get; set; }
|
||||
|
||||
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
|
||||
{
|
||||
configurationBuilder.Properties<PhoneNumber>().HaveConversion<PhoneNumberConverter>();
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder builder)
|
||||
{
|
||||
builder.Entity<Color>(entity =>
|
||||
@ -89,6 +96,7 @@ namespace DAL.Contexts
|
||||
{
|
||||
entity.HasOne(e => e.updaterRelation).WithMany()
|
||||
.HasForeignKey(e => e.updater).HasConstraintName("events_users_id_fk");
|
||||
entity.Property(e => e.hashingType).HasConversion<string>();
|
||||
});
|
||||
|
||||
builder.Entity<AuditColor>(entity =>
|
||||
|
19
DAL/Converters/PhoneNumberConverter.cs
Normal file
19
DAL/Converters/PhoneNumberConverter.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using DAL.Values;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
namespace DAL.Converters
|
||||
{
|
||||
public class PhoneNumberConverter : ValueConverter<PhoneNumber, string>
|
||||
{
|
||||
public PhoneNumberConverter() : base(
|
||||
v => v.value,
|
||||
v => new PhoneNumber
|
||||
{
|
||||
value = v
|
||||
}
|
||||
)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
using DAL.Values;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
@ -17,7 +18,7 @@ namespace DAL.Models.Audits
|
||||
public string lastName { get; set; } = null!;
|
||||
|
||||
[Column("phoneNumber")]
|
||||
public ulong phoneNumber { get; set; }
|
||||
public PhoneNumber phoneNumber { get; set; } = null!;
|
||||
|
||||
[Column("hashingType")]
|
||||
[MaxLength(64)]
|
||||
|
@ -1,4 +1,5 @@
|
||||
using DAL.Models.Audits;
|
||||
using DAL.Values;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
@ -26,7 +27,8 @@ namespace DAL.Models
|
||||
public string lastName { get; set; } = null!;
|
||||
|
||||
[Column("phoneNumber")]
|
||||
public ulong phoneNumber { get; set; }
|
||||
[MaxLength(32)]
|
||||
public PhoneNumber phoneNumber { get; set; } = null!;
|
||||
|
||||
[Column("password")]
|
||||
[MaxLength(1000)]
|
||||
|
71
DAL/Values/PhoneNumber.cs
Normal file
71
DAL/Values/PhoneNumber.cs
Normal file
@ -0,0 +1,71 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace DAL.Values
|
||||
{
|
||||
[JsonConverter(typeof(PhoneNumberJsonConverter))]
|
||||
public partial class PhoneNumber
|
||||
{
|
||||
[MaxLength(32)] private string _phoneNumber = null!;
|
||||
|
||||
[JsonIgnore]
|
||||
[MaxLength(32)]
|
||||
public string value
|
||||
{
|
||||
get => _phoneNumber;
|
||||
set
|
||||
{
|
||||
if (phoneNumberConfirmation().IsMatch(value))
|
||||
{
|
||||
_phoneNumber = phoneNumberConfirmation().Replace(value, "$1 ($2) $3-$4");
|
||||
_phoneNumber = _phoneNumber.Trim();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ValidationException("Phone number is invalid");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static implicit operator string(PhoneNumber phoneNumber)
|
||||
{
|
||||
return phoneNumber.value;
|
||||
}
|
||||
public static implicit operator PhoneNumber(string str)
|
||||
{
|
||||
return new PhoneNumber { value = str };
|
||||
}
|
||||
|
||||
// https://www.abstractapi.com/guides/phone-validation/c-validate-phone-number
|
||||
[GeneratedRegex(@"^([+]?[0-9]{1,3})?[-. ]?\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$")]
|
||||
public static partial Regex phoneNumberConfirmation();
|
||||
}
|
||||
|
||||
public class PhoneNumberJsonConverter : JsonConverter<PhoneNumber>
|
||||
{
|
||||
public override PhoneNumber? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
string? phoneNumber = reader.GetString();
|
||||
if (phoneNumber != null)
|
||||
{
|
||||
if (!PhoneNumber.phoneNumberConfirmation().IsMatch(phoneNumber))
|
||||
{
|
||||
throw new JsonException("Phone number invalid. Match '^([+]?[0-9]{1,3})?[-. ]?\\(?([0-9]{3})\\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$'");
|
||||
}
|
||||
|
||||
return new PhoneNumber
|
||||
{
|
||||
value = phoneNumber
|
||||
};
|
||||
}
|
||||
|
||||
throw new JsonException("Expected string for PhoneNumber deserialization.");
|
||||
}
|
||||
public override void Write(Utf8JsonWriter writer, PhoneNumber value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStringValue(value);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user