/// <summary> /// Map CourseAdd_DTO to CourseDB, passes to Add DB-method the CourseDB /// </summary> /// <param name="model"></param> /// <returns></returns> public async Task <Result <Course> > AddCourseAsync(CourseAdd_DTO model) { var courseDb = _mapper.Map <CourseAdd_DTO, CourseDB>(model); _uow.Courses.Add(courseDb); try { await _uow.SaveAsync(); return(Result <Course> .Ok(_mapper.Map <Course>(courseDb))); } catch (DbUpdateConcurrencyException e) { return((Result <Course>) Result <Course> .Fail("Cannot save model")); } catch (DbUpdateException e) { return((Result <Course>) Result <Course> .Fail($"Cannot save model. Duplicate field. {e.Message}")); } catch (DbEntityValidationException e) { return((Result <Course>) Result <Course> .Fail("Invalid model")); } }
public async Task <IHttpActionResult> CreateCourseAsync ([FromBody, CustomizeValidator] CourseAdd_DTO course) { if (course is null || !ModelState.IsValid) { return(BadRequest(ModelState)); } var result = await _courseBase.AddCourseAsync(course); return(result.IsError ? BadRequest(result.Message) : (IHttpActionResult)Ok(result.Data)); }