public async Task <IActionResult> AddNewRecipe(int userId, [FromBody] RecipeForCreateDto recipeForCreateDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _userRepository.GetUser(userId); recipeForCreateDto.Name = recipeForCreateDto.Name.ToLower(); recipeForCreateDto.AuthorId = userId; if (await _recipesRepository.RecipeExists(recipeForCreateDto.Name)) { return(BadRequest("Recipe with that name already exists!")); } var recipeToCreate = _mapper.Map <Recipe>(recipeForCreateDto); recipeToCreate.UserId = userId; var createdRecipe = await _recipesRepository.AddNewRecipe(recipeToCreate); var recipeToReturn = _mapper.Map <RecipeForDetailDto>(createdRecipe); return(CreatedAtRoute("GetUser", new { controller = "Users", userId = userId, id = createdRecipe.Id }, recipeToReturn)); }
public async Task <IActionResult> CreateRecipe(RecipeForCreateDto recipeForCreateDto) { //Get values from token // var identity = HttpContext.User.Identity as ClaimsIdentity; // if (identity != null) // { // IEnumerable<Claim> claims = identity.Claims; // } if (User.FindFirst(ClaimTypes.NameIdentifier) == null) { throw new Exception("Cant find user in token"); } recipeForCreateDto.UserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); var recipe = _mapper.Map <Recipe>(recipeForCreateDto); _repo.Add(recipe); if (await _repo.SaveAll()) { return(StatusCode(201)); } throw new Exception($"the creation for recipe fail"); }