예제 #1
0
        public async Task <ActionResult <StudSubjectsModel> > Post(string studno, StudSubjectsModel model)
        {
            //try
            //{
            var student = await _repo.GetStudentsRec(studno);

            if (student == null)
            {
                return(BadRequest("Student does not exist"));
            }

            var StudentSubs = _mapper.Map <StudentSubjectsTbl>(model);

            StudentSubs.StudentNo = student;

            //if (model.StudId == 0) return BadRequest("Student ID is required");
            if (model.SubId == 0)
            {
                return(BadRequest("Subject ID is required"));
            }

            var subjects = await _repo.GetSubject(model.SubId);

            if (subjects == null)
            {
                return(BadRequest("Subject could not be found"));
            }
            StudentSubs.SubjectInfo = subjects;

            _repo.Add(StudentSubs);

            if (await _repo.SaveChangesAsync())
            {
                var url = _linkGenerator.GetPathByAction(HttpContext,
                                                         "Get",
                                                         values: new { studno, id = StudentSubs.StudentNo });

                return(Created(url, _mapper.Map <StudSubjectsModel>(StudentSubs)));
            }
            else
            {
                return(BadRequest("Failed to save new Subject"));
            }
            //}
            //catch (Exception)
            //{
            //    return StatusCode(StatusCodes.Status500InternalServerError, "Failed to get Talks");
            //}
        }
예제 #2
0
        public async Task <ActionResult <StudSubjectsModel> > Put(string studno, int subjectID, StudSubjectsModel model)
        {
            //try
            //{
            var subject = await _repo.GetSubByStudno(studno, subjectID, true);

            if (subject == null)
            {
                return(NotFound("Couldn't find the subject"));
            }

            _mapper.Map(model, subject);

            if (model.SubId != 0)
            {
                var subsInfo = await _repo.GetSubject(model.SubId);

                if (subsInfo != null)
                {
                    subject.SubjectInfo = subsInfo;
                }
            }

            if (await _repo.SaveChangesAsync())
            {
                return(_mapper.Map <StudSubjectsModel>(subject));
            }
            else
            {
                return(BadRequest("Failed to update database."));
            }
            //}
            //catch (Exception)
            //{
            //    return StatusCode(StatusCodes.Status500InternalServerError, "Failed to get Talks");
            //}
        }