/// <summary> /// Map UserAdd_DTO to StudentDB, passes to Add DB-method the StudentDB /// </summary> /// <param name="model"></param> /// <returns></returns> public async Task <Result <Student> > AddStudentAsync(UserAdd_DTO model) { var studentDb = _mapper.Map <UserAdd_DTO, StudentDB>(model); _uow.Students.Add(studentDb); try { await _uow.SaveAsync(); return(Result <Student> .Ok(_mapper.Map <Student>(studentDb))); } catch (DbUpdateConcurrencyException e) { return((Result <Student>) Result <Student> .Fail("Cannot save model")); } catch (DbUpdateException e) { return((Result <Student>) Result <Student> .Fail($"Cannot save model. Duplicate field. {e.Message}")); } catch (DbEntityValidationException e) { return((Result <Student>) Result <Student> .Fail("Invalid model")); } }
public async Task <IHttpActionResult> CreateStudentAsync ([FromBody] UserAdd_DTO user) { if (user is null) { return(BadRequest()); } var result = await _studentService.AddStudentAsync(user); return(result.IsError ? BadRequest(result.Message) : (IHttpActionResult)Ok(result.Data)); }