public async Task <object> Signup([FromBody] User model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var isExistingUser = context.Users.Where(x => x.Email == model.Email); if (isExistingUser.Count() > 0) { ModelState.AddModelError("Email", "The user with email '" + model.Email + "' already exists."); return(BadRequest(ModelState)); } await context.Users.AddAsync(model); await context.SaveChangesAsync(); var user = await context.Users .Include(p => p.Competitions) .ThenInclude(p => p.Competition) .ThenInclude(p => p.CreatedBy) .SingleOrDefaultAsync(p => p.Id == model.Id); return(GetToken(mapper.Map <User, IUserResourceShort>(user))); }
public async Task <IActionResult> CreateCompetition([FromBody] SaveCompetitionResource saveCompetitionResource) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var competition = mapper.Map <SaveCompetitionResource, Competition>(saveCompetitionResource); await context.Competitions.AddAsync(competition); await context.SaveChangesAsync(); competition = await context.Competitions .Include(p => p.CreatedBy) .Include(p => p.Participants) .ThenInclude(p => p.User) .SingleOrDefaultAsync(p => p.Id == competition.Id); return(Ok(mapper.Map <Competition, ICompetitionResource>(competition))); }
public async Task <IActionResult> UpdateUser(int id, [FromBody] SaveUserResource userResource) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = await context.Users .Include(p => p.Competitions) .ThenInclude(p => p.Competition) .ThenInclude(p => p.CreatedBy) .SingleOrDefaultAsync(p => p.Id == id); if (user == null) { return(NotFound()); } mapper.Map(userResource, user); await context.SaveChangesAsync(); return(Ok(mapper.Map <User, IUserResource>(user))); }