public ResultViewModel <UserTrackEditViewModel> Post(UserTrackEditViewModel UserTrack)
        {
            ResultViewModel <UserTrackEditViewModel> result
                = new ResultViewModel <UserTrackEditViewModel>();

            try
            {
                if (!ModelState.IsValid)
                {
                    result.Message = "In Valid Model State";
                }
                else
                {
                    UserTrackEditViewModel selectedUserTrack
                        = UserTrackService.Add(UserTrack);

                    result.Successed = true;
                    result.Data      = selectedUserTrack;
                }
            }
            catch (Exception ex)
            {
                result.Successed = false;
                result.Message   = "Semething Went Wrong";
            }
            return(result);
        }
Ejemplo n.º 2
0
        public ResultViewModel <UserTrackEditViewModel> Enroll(int TrackID)
        {
            ResultViewModel <UserTrackEditViewModel> result = new ResultViewModel <UserTrackEditViewModel>();

            try
            {
                string Token = Request.Headers.Authorization?
                               .Parameter;
                Dictionary <string, string>
                    cliams = SecurityHelper.Validate(Token);
                int UserID = int.Parse(cliams.First(i => i.Key == "ID").Value);

                if (userTrackService.GetUserTracksCount(UserID) > 2)
                {
                    result.Successed = false;
                    result.Message   = "You exceeded the maximum number of uncomplete tracks ";
                }

                else if (!userTrackService.CheckToEnroll(UserID, TrackID))
                {
                    result.Successed = false;
                    result.Message   = "You Already Enrolled in this Track";
                }

                else

                {
                    UserTrackEditViewModel userTrack = userTrackService.Add(new UserTrackEditViewModel()
                    {
                        ID          = 0,
                        TrackID     = TrackID,
                        UserID      = UserID,
                        Date        = DateTime.Now,
                        FinshDate   = DateTime.Now.AddMonths(1),
                        IsApproveed = false
                    });
                    result.Message   = "Enrollment Request Sent successfully";
                    result.Successed = true;
                    result.Data      = userTrack;

                    var track = trackService.GetByID(TrackID);
                    var user  = userService.GetByID(UserID);

                    var returnedData = new
                    {
                        TrackName = track.Name,
                        Date      = DateTime.Now,
                        UserName  = user.Name,
                        UserID    = user.ID
                    };
                    Hub.Clients.All.GetEnrolledTrack(returnedData);
                }
            }
            catch (Exception e)
            {
                result.Successed = false;
                result.Message   = "Error";
            }

            return(result);
        }