Esempio n. 1
0
        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());
        }