public JsonResult Post(string planName, [FromBody] Course vm) { try { if (ModelState.IsValid) { // Map to the Entity var newCourse = Mapper.Map <Course>(vm); // Save to the db _repository.AddNewCourseToPlan(planName, User.Identity.Name, newCourse); if (_repository.SaveAll()) { Response.StatusCode = (int)HttpStatusCode.Created; return(Json(Mapper.Map <CourseViewModel>(newCourse))); } } } catch (Exception ex) { _logger.LogError("Failed to save new course", ex); Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json("Failed to save new course")); } Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json("Validation failed on new course")); }
public JsonResult Post([FromBody] LearningPlanViewModel vm) { try { if (ModelState.IsValid) { var newPlan = Mapper.Map <LearningPlan>(vm); newPlan.UserName = User.Identity.Name; // Save to the db _logger.LogInformation("Attempting to save a new plan"); _repository.AddLearningPlan(newPlan); if (_repository.SaveAll()) { Response.StatusCode = (int)HttpStatusCode.Created; return(Json(Mapper.Map <LearningPlanViewModel>(newPlan))); } } } catch (Exception ex) { _logger.LogError("Failed to save new plan", ex); Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(new { Message = ex.Message })); } Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(new { Message = "Failed", ModelState = ModelState })); }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreationDto) { // if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) // return Unauthorized(); var userFromRepo = await _repo.GetUser(userId); var file = photoForCreationDto.File; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation() .Width(500).Height(500).Crop("fill").Gravity("face") }; uploadResult = _cloudinary.Upload(uploadParams); } } photoForCreationDto.Url = uploadResult.Uri.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoForCreationDto); // if (!userFromRepo.Photos.Any(u => u.IsMain)) photo.IsMain = true; userFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn)); } return(BadRequest("Could not add the photo")); }
public async Task <IActionResult> UpdateUser(int id, UserForUpdateDto userForUpdateDto) { // if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) // return Unauthorized(); var userFromRepo = await _repo.GetUser(id); _mapper.Map(userForUpdateDto, userFromRepo); if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception($"Updating user {id} failed on save"); }