public IActionResult UpdateAuthorities(int roomId, List <LehrerRaum> authorities) { if (!TokenProvider.IsAdmin(User)) { return(Unauthorized()); } if (authorities == null) { return(BadRequest()); } if (authorities.Any(a => a.RaumId != roomId)) { return(BadRequest()); } var dbAuthorities = _context.LehrerRaum.Where(lr => lr.RaumId == roomId); foreach (var authority in authorities) { var dbAuthority = dbAuthorities.FirstOrDefault(lr => lr.LehrerId == authority.LehrerId && lr.RaumId == authority.RaumId); if (dbAuthority != null) { if (!dbAuthority.Betreuer) { dbAuthority.Betreuer = true; _context.LehrerRaum.Update(dbAuthority); } } else { dbAuthority = new LehrerRaum { RaumId = authority.RaumId, Betreuer = true, LehrerId = authority.LehrerId }; _context.LehrerRaum.Add(dbAuthority); } } foreach (var dbAuthority in dbAuthorities) { if (!authorities.Any(a => a.LehrerId == dbAuthority.LehrerId && a.RaumId == dbAuthority.RaumId)) { _context.LehrerRaum.Remove(dbAuthority); } } return(Ok()); }
public IActionResult MarkAsAuthority(int teacherId, int roomId) { if (!TokenProvider.IsAdmin(User)) { return(Unauthorized()); } var lehrer = context.Lehrer.FirstOrDefault(r => r.Id == teacherId); if (lehrer == null) { return(NotFound()); } var room = context.Raum.FirstOrDefault(r => r.Id == roomId); if (room == null) { return(NotFound()); } var teacherRoom = room.LehrerRaum.FirstOrDefault(l => l.Id == teacherId); if (teacherRoom == null) { teacherRoom = new LehrerRaum(); teacherRoom.LehrerId = teacherId; teacherRoom.RaumId = roomId; teacherRoom.Betreuer = true; context.LehrerRaum.Add(teacherRoom); } else { teacherRoom.Betreuer = true; context.LehrerRaum.Update(teacherRoom); } return(Ok()); }