public async Task <ActionResult <User> > Register([FromBody] User user)
        {
            try {
                if (user == null)
                {
                    return(BadRequest("user object is null".ToBadRequest()));
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState.ToBadRequest()));
                }

                bool isEmailAlreadyTaken = await _context.Users.FirstOrDefaultAsync(u => u.Email == user.Email) != null;

                if (isEmailAlreadyTaken)
                {
                    return(BadRequest("user with this email already exist".ToBadRequest()));
                }

                user.SetPasswordhHash();
                _context.Add(user);
                await _context.SaveChangesAsync();

                var response = new {
                    data = user.ToMessage()
                };
                return(Created("register", response));
            } catch (Exception e) {
                return(StatusCode(500));
            }
        }
Exemple #2
0
        public async Task <IActionResult> Create([Bind("ID,Name")] Athlete athlete)
        {
            if (ModelState.IsValid)
            {
                _context.Add(athlete);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(athlete));
        }
        public async Task <IActionResult> Create([Bind("Id,UserName,Password")] User user)
        {
            if (ModelState.IsValid)
            {
                _context.Add(user);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(user));
        }
Exemple #4
0
        public async Task <IActionResult> Create([Bind("Id,Icon,NameTopic,IsActive")] Topic topic)
        {
            if (ModelState.IsValid)
            {
                _context.Add(topic);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(topic));
        }
Exemple #5
0
        public async Task <IActionResult> Create([Bind("Id,NameExerciseType")] ExerciseType exerciseType)
        {
            if (ModelState.IsValid)
            {
                _context.Add(exerciseType);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(exerciseType));
        }
        public async Task <IActionResult> Create([Bind("Id,ExerciseTypeId,Question,Media,TrueAnswer")] Exercise exercise)
        {
            if (ModelState.IsValid)
            {
                _context.Add(exercise);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ExerciseTypeId"] = new SelectList(_context.ExerciseTypes, "Id", "Id", exercise.ExerciseTypeId);
            return(View(exercise));
        }
        public void addUser(UserDto user)
        {
            User newUser = new User()
            {
                firstName = user.firstName,
                lastName  = user.lastName,
                age       = user.age
            };

            _dbContext.Add(newUser);
            _dbContext.SaveChanges();
        }
        public async Task <IActionResult> Create([Bind("Id,TopicId,NameLesson,RequireExp")] Lesson lesson)
        {
            if (ModelState.IsValid)
            {
                _context.Add(lesson);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["TopicId"] = new SelectList(_context.Topics, "Id", "Id", lesson.TopicId);
            return(View(lesson));
        }
Exemple #9
0
        public async Task <IActionResult> Create([Bind("Id,UserId,LessonId")] UserLesson userLesson)
        {
            if (ModelState.IsValid)
            {
                _context.Add(userLesson);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["LessonId"] = new SelectList(_context.Lessons, "Id", "Id", userLesson.LessonId);
            ViewData["UserId"]   = new SelectList(_context.Users, "Id", "Password", userLesson.UserId);
            return(View(userLesson));
        }
Exemple #10
0
        public async Task <ActionResult <User> > Register([FromBody] User user)
        {
            try {
                if (user == null)
                {
                    return(BadRequest("user object is null".ToBadRequest()));
                }

                _log.Information("User trying to sign up, Body: {@body} on {date}", user.ToMessage(), DateTime.Now);

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState.ToBadRequest(400)));
                }

                var userWithSameEMail = await _context.Users.FirstOrDefaultAsync(u => u.Email == user.Email);

                if (userWithSameEMail != null)
                {
                    return(BadRequest("Email already taken".ToBadRequest()));
                }

                user.SetPasswordhHash();
                _context.Add(user);
                await _context.SaveChangesAsync();

                _log.Information("User created : {@User} on {date} ", user.ToMessage(), DateTime.Now);
                // Envoi d'un mail
                bool isEmailSend = await MailerSendGrid.Send(user.Email);

                if (isEmailSend)
                {
                    _log.Information("Email was sent for {User} on {date} ", user.Id, DateTime.Now);
                }
                else
                {
                    _log.Information("Email was not sent for {User} on {date} ", user.Id, DateTime.Now);
                }

                return(Created("register", Format.ToMessage(user.ToMessage(), 201)));
            } catch (Exception e) {
                _log.Fatal(e.Message + "on Register User on {Date}", DateTime.Now);
                return(StatusCode(500));
            }
        }