public async Task <OkResponse <CourseDto> > UpdateAsync(CourseUpdateDto dto) { if (dto == null || string.IsNullOrEmpty(dto.Id) || string.IsNullOrEmpty(dto.Name)) { return(OkResponse <CourseDto> .Error(HttpStatusCode.BadRequest, "Model is not valid!")); } var updateCourse = _mapper.Map <Course>(dto); var result = await _courseCollection.FindOneAndReplaceAsync(x => x.Id == updateCourse.Id, updateCourse); if (result == null) { return(OkResponse <CourseDto> .Error(HttpStatusCode.NotFound, "Course is not found.")); } var mapDto = _mapper.Map <CourseDto>(result); if (!string.IsNullOrEmpty(result.CategoryId)) { var category = await _categoryCollection.Find(x => x.Id == result.CategoryId).FirstOrDefaultAsync(); mapDto.Category = _mapper.Map <CategoryDto>(category); } return(OkResponse <CourseDto> .Success(HttpStatusCode.OK, mapDto)); }
public async Task <OkResponse <CategoryDto> > CreateAsync(CategoryCreateDto dto) { if (dto == null || string.IsNullOrEmpty(dto.Name)) { return(OkResponse <CategoryDto> .Error(HttpStatusCode.BadRequest, "Model is not valid.")); } var category = _mapper.Map <Category>(dto); await _categoryCollection.InsertOneAsync(category); var mapDto = _mapper.Map <CategoryDto>(category); return(OkResponse <CategoryDto> .Success(HttpStatusCode.OK, mapDto)); }
public async Task <OkResponse <CourseDto> > CreateAsync(CourseCreateDto dto) { if (dto == null || string.IsNullOrEmpty(dto.Name)) { return(OkResponse <CourseDto> .Error(HttpStatusCode.BadRequest, "Model is not valid!")); } var newCourse = _mapper.Map <Course>(dto); newCourse.CreatedTime = DateTime.Now; await _courseCollection.InsertOneAsync(newCourse); var mapDto = _mapper.Map <CourseDto>(newCourse); return(OkResponse <CourseDto> .Success(HttpStatusCode.OK, mapDto)); }
public async Task <OkResponse <object> > DeleteAsync(string id) { if (string.IsNullOrEmpty(id)) { return(OkResponse <object> .Error(HttpStatusCode.BadRequest, "Id cannot be empty!")); } var result = await _courseCollection.DeleteOneAsync(x => x.Id == id); if (result.DeletedCount > 0) { return(OkResponse <object> .Success(HttpStatusCode.NoContent)); } else { return(OkResponse <object> .Error(HttpStatusCode.NotFound, "Course is not found.")); } }
public async Task <OkResponse <CategoryDto> > GetByIdAsync(string id) { if (string.IsNullOrEmpty(id)) { return(OkResponse <CategoryDto> .Error(HttpStatusCode.BadRequest, "Id cannot be empty!")); } var category = await _categoryCollection.Find(x => x.Id == id).FirstOrDefaultAsync(); if (category == null) { return(OkResponse <CategoryDto> .Error(HttpStatusCode.NotFound, "Category not found!")); } var mapDto = _mapper.Map <CategoryDto>(category); return(OkResponse <CategoryDto> .Success(HttpStatusCode.OK, mapDto)); }
public async Task <OkResponse <CategoryDto> > UpdateAsync(CategoryUpdateDto dto) { if (dto == null || string.IsNullOrEmpty(dto.Id) || string.IsNullOrEmpty(dto.Name)) { return(OkResponse <CategoryDto> .Error(HttpStatusCode.BadRequest, "Model is not valid!")); } var updateCategory = _mapper.Map <Category>(dto); var result = await _categoryCollection.FindOneAndReplaceAsync(x => x.Id == updateCategory.Id, updateCategory); if (result == null) { return(OkResponse <CategoryDto> .Error(HttpStatusCode.NotFound, "Category is not found.")); } var mapDto = _mapper.Map <CategoryDto>(result); return(OkResponse <CategoryDto> .Success(HttpStatusCode.OK, mapDto)); }