Ejemplo n.º 1
0
        public async Task <IActionResult> UpdateMyAccount([FromBody] ParticuliersUpdateDTO particulier)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var email = User.Claims.SingleOrDefault(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").Value;

            var user = await _context.Particuliers.SingleOrDefaultAsync(p => p.Mail.Equals(email));

            if (user == null)
            {
                return(NotFound());
            }

            if (await _context.Particuliers.AnyAsync(x => x.Mail == particulier.Mail && x.Id != user.Id))
            {
                return(Conflict("Mail"));
            }

            if (await _context.Particuliers.AnyAsync(x => x.Pseudo == particulier.Pseudo && x.Id != user.Id))
            {
                return(Conflict("Pseudo"));
            }

            if (particulier.LocaliteCode != null && !await _context.Localites.AnyAsync(x => x.CodePostal.Equals(particulier.LocaliteCode)))
            {
                return(NotFound());
            }

            user = Modification(user, particulier);
            _context.Entry(user).State = EntityState.Modified;

            try {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                return(NotFound());
            }
            return(NoContent());
        }
Ejemplo n.º 2
0
        private Particulier Modification(Particulier initialPart, ParticuliersUpdateDTO targetPart)
        {
            ScryptEncoder encoder = new ScryptEncoder();
            var           retour  = initialPart;

            if (targetPart.Prenom != null)
            {
                retour.Prenom = targetPart.Prenom;
            }
            if (targetPart.Nom != null)
            {
                retour.Nom = targetPart.Nom;
            }
            if (targetPart.Adresse != null)
            {
                retour.Adresse = targetPart.Adresse;
            }
            if (targetPart.Pseudo != null)
            {
                retour.Pseudo = targetPart.Pseudo;
            }
            if (targetPart.DateNaissance != null)
            {
                retour.DateNaissance = targetPart.DateNaissance;
            }
            if (targetPart.MotDePasse != null)
            {
                retour.MotDePasse = encoder.Encode(targetPart.MotDePasse);
            }
            if (targetPart.NumeroTel != null)
            {
                retour.NumeroTel = targetPart.NumeroTel;
            }
            if (targetPart.Mail != null)
            {
                retour.Mail = targetPart.Mail;
            }
            if (targetPart.LocaliteCode != null)
            {
                retour.LocaliteCode = targetPart.LocaliteCode;
            }
            return(retour);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> UpdateParticuliers([FromRoute] Guid id, [FromBody] ParticuliersUpdateDTO particulier)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_context.Particuliers.Any(p => p.Id.Equals(id)))
            {
                return(NotFound());
            }

            if (await _context.Particuliers.AnyAsync(x => x.Mail == particulier.Mail && x.Id != id))
            {
                return(Conflict("Mail"));
            }

            if (await _context.Particuliers.AnyAsync(x => x.Pseudo == particulier.Pseudo && x.Id != id))
            {
                return(Conflict("Pseudo"));
            }

            if (particulier.LocaliteCode != null && !await _context.Localites.AnyAsync(x => x.CodePostal.Equals(particulier.LocaliteCode)))
            {
                return(NotFound());
            }

            var user = await _context.Particuliers.SingleOrDefaultAsync(p => p.Id.Equals(id));

            user = Modification(user, particulier);
            _context.Entry(user).State = EntityState.Modified;

            try {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                return(NotFound());
            }
            return(NoContent());
        }