public async Task <ActionResult <ProjectTask> > PostProjectTask(TaskUser task) { if (!ModelState.IsValid) { return(BadRequest(ErrorFunctionality.ObjectErrorReturn(400, ModelState.Values))); } var authHeader = Request.Headers["Authorization"].ToString(); var user = await _context.Users.FirstOrDefaultAsync(u => "Bearer " + u.Token == authHeader); if (user.TeamId == null) { ModelState.AddModelError("", "Nie posiadasz zespołu, aby dodać zadanie"); return(BadRequest(ErrorFunctionality.ObjectErrorReturn(400, ModelState.Values))); } var userToAssign = await _context.Users.FirstOrDefaultAsync(u => u.Id == task.StudentId && u.TeamId == user.TeamId); if (userToAssign == null) { ModelState.AddModelError("", "Taki użytkownik nie istnieje w twoim zespole"); return(BadRequest(ErrorFunctionality.ObjectErrorReturn(400, ModelState.Values))); } task.ProjectTask.Student = userToAssign; task.ProjectTask.TeamId = user.TeamId; _context.Tasks.Add(task.ProjectTask); await _context.SaveChangesAsync(); return(CreatedAtAction("PostProjectTask", new { id = task.ProjectTask.Id }, task.ProjectTask)); }
public async Task <ActionResult <User> > Register(User user) { if (!ModelState.IsValid) { return(BadRequest(ErrorFunctionality.ObjectErrorReturn(400, ModelState.Values))); } if (user.AccountType != AccountType.stud.ToString() && user.AccountType != AccountType.work.ToString()) { ModelState.AddModelError("", "Błąd przy wyborze rodzaju konta"); return(BadRequest(ErrorFunctionality.ObjectErrorReturn(400, ModelState.Values))); } if (_context.Users.Any(x => x.Email == user.Email)) { ModelState.AddModelError("", "Powtarzający się email"); return(BadRequest(ErrorFunctionality.ObjectErrorReturn(400, ModelState.Values))); } _context.Users.Add(user); await _context.SaveChangesAsync(); return(CreatedAtAction("Register", new { id = user.Id }, user)); }
public async Task <ActionResult <Team> > GetTeamOfStudent() { var authHeader = Request.Headers["Authorization"].ToString(); var user = await _context.Users.FirstOrDefaultAsync(u => "Bearer " + u.Token == authHeader); if (user.Team == null) { ModelState.AddModelError("", "Twój rodzaj konta nie może wykonać tej operacji"); return(BadRequest(ErrorFunctionality.ObjectErrorReturn(400, ModelState.Values))); } return(user.Team); }
public async Task <ActionResult <Topic> > PostTopic(Topic topic) { if (!ModelState.IsValid) { ModelState.AddModelError("", "Niepoprawny model tematu"); return(BadRequest(ErrorFunctionality.ObjectErrorReturn(400, ModelState.Values))); } if (_context.Topics.Any(t => t.Name == topic.Name)) { ModelState.AddModelError("", "Niepoprawna nazwa modelu"); return(BadRequest(ErrorFunctionality.ObjectErrorReturn(400, ModelState.Values))); } _context.Topics.Add(topic); await _context.SaveChangesAsync(); return(CreatedAtAction("GetTopic", new { id = topic.Id }, topic)); }
public async Task <ActionResult <IEnumerable <ProjectTask> > > GetTasks() { var authHeader = Request.Headers["Authorization"].ToString(); var user = await _context.Users.FirstOrDefaultAsync(u => "Bearer " + u.Token == authHeader); var tasks = _context.Tasks.Where(t => t.TeamId == user.TeamId).Include(t => t.Student); if (tasks == null) { ModelState.AddModelError("", "Zespół o takim id nie istnieje"); return(BadRequest(ErrorFunctionality.ObjectErrorReturn(400, ModelState.Values))); } foreach (var task in tasks) { task.Student.Tasks = null; } return(tasks.ToList()); }
public async Task <ActionResult <Team> > PostTeam(TeamTopic tT) { if (!ModelState.IsValid) { return(BadRequest(ErrorFunctionality.ObjectErrorReturn(400, ModelState.Values))); } var authHeader = Request.Headers["Authorization"].ToString(); var student = await _context.Users.FirstOrDefaultAsync(u => "Bearer " + u.Token == authHeader); if (student.AccountType != AccountType.stud.ToString()) { ModelState.AddModelError("", "Twój rodzaj konta nie może wykonać tej operacji"); return(BadRequest(ErrorFunctionality.ObjectErrorReturn(400, ModelState.Values))); } if (student.TeamId != null) { ModelState.AddModelError("", "Już należysz do zespołu!"); return(BadRequest(ErrorFunctionality.ObjectErrorReturn(400, ModelState.Values))); } if (tT.TopicId != null) { var topic = await _context.Topics.FindAsync(tT.TopicId); if (topic == null) { ModelState.AddModelError("", "Taki temat nie istnieje!"); return(BadRequest(ErrorFunctionality.ObjectErrorReturn(400, ModelState.Values))); } tT.Team.Topic = topic; } tT.Team.Students.Add(student); _context.Teams.Add(tT.Team); await _context.SaveChangesAsync(); return(CreatedAtAction("GetTeam", new { id = tT.Team.Id }, tT.Team)); }