コード例 #1
0
        public async Task <IActionResult> EditImage(EditImageViewModel editImageViewModel)
        {
            var updateImageModel = new UpdateImageModel
            {
                Title = editImageViewModel.Title
            };

            var serializedImage = JsonSerializer.Serialize(updateImageModel);

            var httpClient = _clientFactory.CreateClient("Api");

            var request = new HttpRequestMessage(
                HttpMethod.Put, $"/images/{editImageViewModel.Id}")
            {
                Content = new StringContent(
                    serializedImage, Encoding.UTF8, MediaTypeNames.Application.Json)
            };

            var response = await httpClient.SendAsync(
                request, HttpCompletionOption.ResponseHeadersRead);

            response.EnsureSuccessStatusCode();

            return(RedirectToAction("Index"));
        }
コード例 #2
0
        public async Task <IActionResult> Put(Guid id, [FromBody] UpdateImageModel updateImageModel)
        {
            var image = await _context.Images.FindAsync(id);

            image.Title = updateImageModel.Title ?? image.Title;

            _context.Images.Update(image);
            await _context.SaveChangesAsync();

            var imageModel = CreateImageModel(image);

            return(AcceptedAtAction(nameof(Get), new { id = imageModel.Id }, imageModel));
        }
コード例 #3
0
        public IHttpActionResult EditImage(int id, [FromBody] UpdateImageModel model)
        {
            if (id <= 0)
            {
                return(BadRequest("Invalid image id"));
            }

            var tagRegex = model.Tags.All(p =>
            {
                return(Regex.IsMatch(p, @"^\w*$") && p.Length < 20);
            });

            if (tagRegex == false)
            {
                return(BadRequest("Tag should contain only alphanumeric values and should not contain white spaces"));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                string userId = HttpContext.Current.User.Identity.GetUserId();
                imageService.EditImage(id, model.Description, model.Tags, userId);
            }
            catch (ArgumentNullException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.GetBaseException().Message));
            }

            return(Ok("Image was updated"));
        }
コード例 #4
0
        public IActionResult UpdateProfileImage(int userid, [FromBody] UpdateImageModel imageModel)
        {
            var command = new UpdateProfileImageCommand(userid, imageModel);

            return(ProcessCommand(command));
        }
コード例 #5
0
 public UpdateProfileImageCommand(int userid, UpdateImageModel model)
 {
     Model  = model;
     UserId = userid;
 }