示例#1
0
        public async Task <IActionResult> CreateMessage(int userId, [FromBody] MessageForCreationDto messageForCreationDto)
        {
            var sender = await _repo.GetUser(userId);

            if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            messageForCreationDto.SenderId = userId;

            var recipient = await _repo.GetUser(messageForCreationDto.RecipientId);


            if (recipient == null)
            {
                return(BadRequest("Could not find user"));
            }

            var message = _mapper.Map <Message>(messageForCreationDto);

            _repo.Add(message);



            if (await _repo.SaveAll())
            {
                var messageToReturn = _mapper.Map <MessageToReturnDto>(message);

                return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn));
            }


            throw new Exception("Creating the message failed on save");
        }
示例#2
0
        public async Task <IActionResult> CreateGroup(/*int userId, */ [FromBody] GroupForCreationDto groupForCreationDto)
        {
            List <User> volunteers = new List <User>();

            foreach (var volIds in groupForCreationDto.VolunteerIds)
            {
                var volunteer = await _repo.GetUser(volIds);

                volunteers.Add(volunteer);
            }

            List <User> students = new List <User>();

            foreach (var studIds in groupForCreationDto.StudentIds)
            {
                var student = await _repo.GetUser(studIds);

                students.Add(student);
            }

            Group newGroup = new Group();

            newGroup.StudentLevel = students[0].StudentLevel;
            _repo.Add(newGroup);

            foreach (var user in volunteers)
            {
                UserGroup userGroup = new UserGroup
                {
                    // User = user,
                    UserId = user.Id,
                    // Group = newGroup,
                    GroupId = newGroup.Id
                };

                _repo.Add(userGroup);
            }

            foreach (var user in students)
            {
                UserGroup userGroup = new UserGroup
                {
                    // User = user,
                    UserId = user.Id,
                    // Group = newGroup,
                    GroupId = newGroup.Id
                };

                _repo.Add(userGroup);
            }

            if (await _repo.SaveAll())
            {
                return(Ok(newGroup));
            }

            throw new Exception("Creating the assignment failed on save");
        }
示例#3
0
        public async Task <IActionResult> CreateAssignment(int creatorId, [FromBody] AssignmentForCreationDto assignmentForCreationDto)
        {
            var assignmentCreator = await _repo.GetUser(creatorId);

            if (assignmentCreator.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            assignmentForCreationDto.CreatedBy = assignmentCreator;
            var assignment = _mapper.Map <Assignment>(assignmentForCreationDto);

            if (assignment.Assigned)
            {
                assignment.DateAssigned = DateTime.Now;
            }

            var students = _repo.GetStudents(assignmentForCreationDto.StudentLevel);

            _repo.Add(assignment);

            foreach (var student in students.Result)
            {
                var userAssignment = new UserAssignment()
                {
                    UserId       = student.Id,
                    AssignmentId = assignment.Id,
                    Completed    = false
                };

                if (!student.UserAssignments.Contains(userAssignment))
                {
                    _repo.Add(userAssignment);
                }
            }

            if (await _repo.SaveAll())
            {
                // _repo.SqlCmdHelper("SET IDENTITY_INSERT dbo.UserAssignments OFF");
                var assignmentToReturn = _mapper.Map <AssignmentToReturnDto>(assignment);
                return(CreatedAtRoute("GetAssignment", new { id = assignment.Id }, assignmentToReturn));
            }

            throw new Exception("Creating the assignment failed on save");
        }
示例#4
0
        public async Task <IActionResult> CreateEvent(int creatorId, [FromBody] EventForCreationDto eventForCreationDto)
        {
            var eventCreator = await _repo.GetUser(creatorId);

            // if (eventCreator.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            //     return Unauthorized();

            var evnt = _mapper.Map <Event>(eventForCreationDto);

            evnt.CreatedBy = eventCreator;

            _repo.Add(evnt);

            if (await _repo.SaveAll())
            {
                var eventToReturn = _mapper.Map <EventToReturnDto>(evnt);
                return(CreatedAtRoute("GetEvent", new { id = evnt.Id }, eventToReturn));
            }

            throw new Exception("Creating the assignment failed on save");
        }
示例#5
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());
        }
        public async Task <IActionResult> UpdateUserAssignment(int id, UserAssignment userAssignment)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userAssignmentFromRepo = await _repo.GetUserAssignment(userAssignment.AssignmentId);

            userAssignmentFromRepo.Completed = userAssignment.Completed;

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Updating UserAssignment {id} failed on save");
        }
示例#7
0
        public async Task <IActionResult> DeleteUser(int userId, int id)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var user = await _repo.GetUser(id);

            _repo.Delete(user);

            if (await _repo.SaveAll())
            {
                return(Ok());
            }

            return(BadRequest("Failed To Delete User"));
        }