예제 #1
0
        public async Task <ActionResult> AddUser([FromBody] User user)
        {
            if (ModelState.IsValid)
            {
                User dbUser = null; //assume user does not exist

                dbUser = _context.Users.FirstOrDefault(x => x.Username == user.Username);

                //if user aready exist return
                if (dbUser != null)
                {
                    return(new BadRequestObjectResult("User already exists"));
                }
                //get today's date and save it under user entry date
                user.EntryDate = DateTime.Now;
                //encrypt password
                user.Password = Security.HashSensitiveData(user.Password);
                //generate token
                string tokenString = BuildToken();
                user.Token = tokenString;
                //Save User
                await _context.AddAsync(user);

                _context.SaveChanges();

                new OkObjectResult("User saved successfully");
            }
            else
            {
                return(new BadRequestObjectResult("user cannot be null"));
            }
            return(Ok(user));
        }
예제 #2
0
        public async Task <IActionResult> AddCompetition([FromBody] Competition comp)
        {
            if (ModelState.IsValid)
            {
                Competition dbCompetition = _context.Competitions.FirstOrDefault(c => c.Name == comp.Name);
                if (dbCompetition != null)
                {
                    return(new BadRequestObjectResult(comp.Name + " already exists"));
                }
                _notificationMessages.CompetitionNotification(_context, comp);
                await _context.AddAsync(comp);

                await _context.SaveChangesAsync();

                return(Ok("Competition Added!"));
            }
            else
            {
                return(new BadRequestObjectResult("competition cannot be null"));
            }
        }