예제 #1
0
        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));
        }
예제 #2
0
        public async Task <OkResponse <List <CategoryDto> > > GetAllAsync()
        {
            var categories = await _categoryCollection.Find(x => true).ToListAsync();

            var mapDtos = _mapper.Map <List <CategoryDto> >(categories);

            return(OkResponse <List <CategoryDto> > .Success(HttpStatusCode.OK, mapDtos));
        }
예제 #3
0
        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));
        }
예제 #4
0
        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));
        }
예제 #5
0
        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."));
            }
        }
예제 #6
0
        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));
        }
예제 #7
0
        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));
        }
예제 #8
0
        public async Task <OkResponse <List <CourseDto> > > GetAllAsync(string userId = null)
        {
            var courses = await _courseCollection.Find(x => string.IsNullOrEmpty(userId) || x.UserId == userId).ToListAsync();

            if (!courses.Any())
            {
                courses = new List <Course>();
            }
            else
            {
                var categoryIds = courses.Select(x => x.CategoryId).ToList();
                var categories  = await _categoryCollection.Find(x => categoryIds.Contains(x.Id)).ToListAsync();

                if (categoryIds.Any())
                {
                    courses.ForEach(item => item.Category = categories.FirstOrDefault(x => x.Id == item.CategoryId));
                }
            }

            var mapDtos = _mapper.Map <List <CourseDto> >(courses);

            return(OkResponse <List <CourseDto> > .Success(HttpStatusCode.OK, mapDtos));
        }