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> PutUser([FromRoute] int id, [FromBody] User user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != user.UserID)
            {
                return(BadRequest());
            }

            _context.Entry(user).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemple #3
0
        public async Task <IActionResult> PutProduct(int id, Product product)
        {
            if (id != product.Id)
            {
                return(BadRequest());
            }

            _context.Entry(product).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemple #4
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 #6
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 #7
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 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 #10
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 #11
0
        public async Task <ActionResult> Put(Guid id, [FromBody] Tag tag)
        {
            try {
                _log.Information("Add tag to Books requested on {Date}", DateTime.Now);
                var tagFromDb = await _context.Tags.FirstOrDefaultAsync(x => x.Name == tag.Name);

                var bookFromDb = await _context.Books.FirstOrDefaultAsync(x => x.Id == id);

                if (bookFromDb == null)
                {
                    return(BadRequest("Book not found".ToBadRequest()));
                }

                if (tagFromDb != null)
                {
                    var asso = await _context.Taggeds.FirstOrDefaultAsync(x => x.BookId == id || x.TagId == tagFromDb.Id);

                    if (asso != null)
                    {
                        return(Ok(Format.ToMessage("Success", 200)));
                    }

                    var newAssociation = new Tagged {
                        TagId = tagFromDb.Id, BookId = id
                    };
                    _context.Taggeds.Add(newAssociation);
                    await _context.SaveChangesAsync();

                    return(Ok(Format.ToMessage("Success", 200)));
                }

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

                _context.Tags.Add(tag);
                await _context.SaveChangesAsync();

                var newTagged = new Tagged {
                    TagId = tag.Id, BookId = id
                };
                _context.Taggeds.Add(newTagged);
                await _context.SaveChangesAsync();

                return(Ok(Format.ToMessage("Success", 200)));
            } catch (Exception e) {
                _log.Fatal(e.Message + "on Get Books on {Date}", DateTime.Now);
                return(StatusCode(500));
            }
        }
Exemple #12
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));
            }
        }
Exemple #13
0
 public ActionResult InvoiceForm(InvoiceModel newInvoice, int id)
 {
     if (ModelState.IsValid)
     {
         var invoice = db.Invoices.Find(id);
         invoice.Status    = "Paid";
         invoice.Date      = DateTime.Today;
         invoice.Card      = newInvoice.Card;
         invoice.CardOwner = newInvoice.CardOwner;
         invoice.CVC       = newInvoice.CVC;
         invoice.Amount    = newInvoice.Amount;
         db.SaveChangesAsync();
         this.Flash("success", "Added!");
         return(RedirectToAction("RentList", "Rent"));
     }
     this.Flash("error", "Failed!");
     return(View());
 }