From 107c68eb4e501c970adbfe55942879f742078d1c Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Sun, 6 Nov 2022 09:10:41 -0800 Subject: [PATCH] eh lala --- .../Controllers/UserController.cs | 22 ++++++++++++++++++- .../GrossesMitainesAPI/Data/InventoryUser.cs | 1 - .../Models/SignUpUserModel.cs | 15 ++++++++++++- .../src/components/Topbar.js | 6 ++--- .../grosses-mitaines-ui/src/pages/Register.js | 12 +++++++++- 5 files changed, 49 insertions(+), 7 deletions(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/UserController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/UserController.cs index e789d86..a5153d9 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/UserController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/UserController.cs @@ -40,13 +40,33 @@ public class UserController : Controller { #region API Methods [HttpPost, AllowAnonymous] public ActionResult Post(SignUpUserModel sign) { + int x = 0; InventoryUser usr; try { - usr = new(sign); + usr = new() { + FirstName = sign.FirstName, + LastName = sign.LastName, + Email = sign.Email, + PhoneNumber = sign.Phone + }; } catch (Exception e){ return BadRequest($"Erreur utilisateur: {e.Message}"); } + try { + usr.Adresses.Add(new AddressModel() { + CivicNumber = sign.CivicNumber, + Appartment = sign.Appartment, + Street = sign.Street, + City = sign.City, + Province = sign.Province, + Country = sign.Country, + PostalCode = sign.PostalCode + }); + } catch (Exception e) { + return BadRequest($"Erreur adresse: {e.Message}"); + } + try { usr.PasswordHash = new PasswordHasher().HashPassword(usr, sign.Password); } catch (Exception e){ diff --git a/GrossesMitaines/GrossesMitainesAPI/Data/InventoryUser.cs b/GrossesMitaines/GrossesMitainesAPI/Data/InventoryUser.cs index e2882d1..5e36b0d 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Data/InventoryUser.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Data/InventoryUser.cs @@ -19,7 +19,6 @@ public class InventoryUser : IdentityUser { NormalizedEmail = sign.Email.ToUpper(); Email = sign.Email; PhoneNumber = sign.Phone; - Adresses = sign.Adresses; } } diff --git a/GrossesMitaines/GrossesMitainesAPI/Models/SignUpUserModel.cs b/GrossesMitaines/GrossesMitainesAPI/Models/SignUpUserModel.cs index f8a3763..6913b56 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Models/SignUpUserModel.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Models/SignUpUserModel.cs @@ -12,6 +12,19 @@ public class SignUpUserModel { public string Phone { get; set; } [Required] public string Password { get; set; } - public List Adresses { get; set; } + [Required, Range(1, int.MaxValue)] + public int CivicNumber { get; set; } + public string? Appartment { get; set; } + [Required, MinLength(3), MaxLength(50)] + public string Street { get; set; } + [Required, MinLength(4), MaxLength(50)] + public string City { get; set; } + [Required, MaxLength(3)] + public string Province { get; set; } + [Required, MinLength(4), MaxLength(30)] + public string Country { get; set; } + // Source pour regex: https://stackoverflow.com/questions/15774555/efficient-regex-for-canadian-postal-code-function + //[Required, RegularExpression(@"/^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][ -]?\d[ABCEGHJ-NPRSTV-Z]\d$/i")] TODO: REGEX + public string PostalCode { get; set; } } diff --git a/GrossesMitaines/grosses-mitaines-ui/src/components/Topbar.js b/GrossesMitaines/grosses-mitaines-ui/src/components/Topbar.js index b8d888f..aa4c67a 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/components/Topbar.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/components/Topbar.js @@ -10,6 +10,7 @@ import { useCookies } from "react-cookie"; const Topbar = () => { const [cookies, setCookie, removeCookie] = useCookies(['name']); const [user, setLogin] = useState(null); + useEffect(() => { async function reset() { await setLogin(await cookies.GMGM ?? null); @@ -22,9 +23,8 @@ const Topbar = () => { method: 'POST', credentials: 'include' }) - - await removeCookie("GMGM"); - await setLogin(null); // Y U NO WORK?!? + await removeCookie("GMGM"); + await setLogin(null); // Y U NO WORK?!? } return ( diff --git a/GrossesMitaines/grosses-mitaines-ui/src/pages/Register.js b/GrossesMitaines/grosses-mitaines-ui/src/pages/Register.js index 700d2de..5fc7d0a 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/pages/Register.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/pages/Register.js @@ -25,7 +25,17 @@ import { useForm } from "react-hook-form"; export default function App() { const { register, handleSubmit, watch, formState: { errors } } = useForm(); - const onSubmit = data => console.log(data); + const onSubmit = data => { + const response = fetch(`https://localhost:7292/api/User`, { + method: 'POST', + headers: { + 'Accept': 'text/json', + 'Content-Type': 'text/json' + }, + body: JSON.stringify(data) + }) + + } return (