public async Task <IActionResult> CreateStudent(AddStudentDto model)
        {
            try
            {
                var student = _mapper.Map <Student>(model);
                if (!await _manager.CreateStudent(student))
                {
                    return(Ok(new JsonMessageResult("创建失败!", 0, null)));
                }

                return(CreatedAtAction(nameof(GetStudent), new { student.Id }, student));
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(Ok(new JsonMessageResult(e.Message, 0, null)));
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create([FromBody] AddStudentDto model)
        {
            if (!ModelState.IsValid)
            {
                return(CustomResponse(ModelState));
            }
            try
            {
                var entity = new Student(model.Name, model.BirthDate);

                _uow.StudentRepository.Add(entity);

                await _uow.CommitAsync();

                return(CustomResponse(_mapper.Map <StudentDto>(entity)));
            }
            catch (Exception ex)
            {
                return(CustomResponse(ex));
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> AddStudentAsync(AddStudentDto addStudentDto)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var studentModel = _mapper.Map <Student>(addStudentDto);
                    await _context.Add(studentModel);

                    var readStudentDto = _mapper.Map <ReadStudentDto>(studentModel);

                    return(Ok("Studenti eshte regjistruar me sukses!"));
                }
                catch (Exception ex)
                {
                    return(BadRequest(new DataError("Regjistrimi deshtoi " + ex.Message)));
                }
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }