Ejemplo n.º 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");
            //}
        }
Ejemplo n.º 2
0
        //[HttpGet("{studno}")]
        //[MapToApiVersion("1.1")]
        //public async Task<ActionResult<StudentsModel>> Get11(string studno, bool includeSubjects = false)
        //{
        //    //try
        //    //{
        //    var result = await _repo.GetStudentsRecByStudno(studno, includeSubjects);

        //    if (result == null) return NotFound();

        //    return _mapper.Map<StudentsModel>(result);

        //    //}
        //    //catch (Exception)
        //    //{
        //    //    return this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure -moniker");
        //    //}
        //}

        // POST api/students
        public async Task <ActionResult <StudentsModel[]> > Post(StudentsModel model)
        {
            //try
            //{
            var existing = await _repo.GetStudentsRec(model.Studno);

            if (existing != null)
            {
                return(BadRequest("Student No. in Use"));
            }

            var dstudno = _linkGenerator.GetPathByAction("Get",
                                                         "Students",
                                                         new { studno = model.Studno });

            if (string.IsNullOrWhiteSpace(dstudno))
            {
                return(BadRequest("Could not use current Student No."));
            }

            //// Create a new Camp
            var StudentInfo = _mapper.Map <StudentInfoTbl>(model);

            _repo.Add(StudentInfo);
            if (await _repo.SaveChangesAsync())
            {
                return(Created($"/api/sutdents/{StudentInfo.Studno}", _mapper.Map <StudentsModel>(StudentInfo)));
            }

            //}
            //catch (Exception)
            //{
            //    return this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure - Post");
            //}

            return(BadRequest());
        }