Пример #1
0
        public async Task <IActionResult> UpdateTutorial([FromRoute] Guid id, [FromBody] CreateOrUpdateTutorialRequest request)
        {
            Guid.TryParse(User.FindFirst("id")?.Value, out var userId);

            var updatedTutorial = await _tutorialService.UpdateTutorialAsync(id, userId, request);

            if (updatedTutorial != null)
            {
                return(Ok(_mapper.Map <TutorialShortResponse>(updatedTutorial)));
            }
            return(NotFound());
        }
Пример #2
0
        public async Task <IActionResult> CreateTutorial([FromBody] CreateOrUpdateTutorialRequest request)
        {
            Guid.TryParse(User.FindFirst("id")?.Value, out var userId);

            var createdTutorial = await _tutorialService.CreateTutorialAsync(userId, request);

            if (createdTutorial != null)
            {
                return(Created(ApiHelper.GetResourceUri(HttpContext.Request, ApiRoutes.Tutorial.Get, createdTutorial.Id), _mapper.Map <TutorialShortResponse>(createdTutorial)));
            }
            return(BadRequest());
        }
Пример #3
0
        public async Task <Tutorial> UpdateTutorialAsync(Guid id, Guid userId, CreateOrUpdateTutorialRequest request)
        {
            var tutorial = await _tutorials.GetAsync(id);

            if (tutorial == null)
            {
                return(null);
            }

            if (!await UserIsOwner(userId, tutorial.UserId))
            {
                return(null);
            }

            tutorial.Update(_mapper.Map <Tutorial>(request));
            return(await _tutorials.UpdateAsync(tutorial));
        }
Пример #4
0
        public async Task <Tutorial> CreateTutorialAsync(Guid userId, CreateOrUpdateTutorialRequest request)
        {
            var tutorial = _mapper.Map <Tutorial>(request);

            tutorial.UserId    = userId;
            tutorial.CreatedAt = DateTime.UtcNow;
            tutorial.UpdatedAt = DateTime.UtcNow;

            foreach (var categoryId in request.Categories)
            {
                var category = await GetTutorialCategoryAsync(categoryId);

                tutorial.Categories.Add(category);
            }
            ;

            return(await _tutorials.AddAsync(tutorial));
        }