public async Task <IActionResult> AddSession(SessionForCreationDto dto) { var session = _mapper.Map <Session>(dto); int userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); var user = await _repo.GetUser(userId); session.CreatedBy = user.KnownAs; _repo.Add(session); if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Could not add the session")); }
public async Task <IActionResult> CreateSession(int id, [FromBody] SessionForCreationDto sessionForCreationDto) { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } // sessionForCreationDto.DayOfSession = null; if (sessionForCreationDto.DayOfSession == null) { sessionForCreationDto.DayOfSession = DateTime.Today.AddDays(((int)DayOfWeek.Saturday) - (int)DateTime.Today.DayOfWeek); } UserParams userParamsForDelete = new UserParams(); userParamsForDelete.StudentLevel = sessionForCreationDto.StudentLevel; userParamsForDelete.OlderSessionsDelete = true; var sessionsToDelete = await _repo.GetSessions(userParamsForDelete); var session = _mapper.Map <Session>(sessionForCreationDto); _repo.Add(session); foreach (var assignment in sessionForCreationDto.Assignments) { SessionAssignment sessionAssignment = new SessionAssignment { SessionId = session.Id, AssignmentId = assignment.Id }; _repo.Add(sessionAssignment); } if (await _repo.SaveAll()) { var sessionToReturn = _mapper.Map <SessionToReturnDto>(session); return(CreatedAtRoute("GetSession", new { id = session.Id }, sessionToReturn)); } // add imapper profile // map to obj return(Ok()); }