예제 #1
0
        public IActionResult CreateInstitute([FromBody] InstituteCreateDto institute)
        {
            if (institute == null)
            {
                return(BadRequest());
            }

            if (_instituteRepository.isInstituteExist(institute.Name))
            {
                ModelState.AddModelError(nameof(InstituteCreateDto), "Institute Name Already Exist");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var instituteEntity = Mapper.Map <Institute>(institute);

            _instituteRepository.Add(instituteEntity);
            if (!_instituteRepository.Commit())
            {
                throw new Exception("Creating an Institute Failed");
            }

            var instituteReturn = Mapper.Map <InstituteViewModel>(instituteEntity);

            return(CreatedAtRoute("GetInstitute", new { id = instituteReturn.Id }, instituteReturn));
        }