public async Task <IActionResult> Edit(int id, [FromBody] EditUserDto user) { if (ModelState.IsValid) { var existing = await uow.User.Get(id); if (existing == null) { return(NotFound(new { respose = "error", message = $"user id = {id} does not exist" })); } existing.Name = user.Name; existing.Surname = user.Surname; existing.BirthDate = user.BirthDate; existing.CompanyId = user.CompanyId; existing.Email = user.Email; var updated = uow.User.Update(existing); await uow.CommitAsync(); var userPic = await uow.Media.GetUserPic(id); var userDto = mapper.Map <GetUserDto>(updated); userDto.PicUrl = userPic == null ? null : Url.ActionUserPic(userPic.BlobPath); return(Ok(userDto)); } return(BadRequest(ModelState)); }
public async Task <IActionResult> Get(int id) { if (ModelState.IsValid) { var user = await uow.User.Get(id); if (user == null) { return(NotFound(new { respose = "error", message = $"user id = {id} does not exist" })); } var userPic = await uow.Media.GetUserPic(id); var userDto = mapper.Map <GetUserDto>(user); userDto.PicUrl = userPic == null ? null : Url.ActionUserPic(userPic.BlobPath); return(Ok(userDto)); } return(BadRequest(ModelState)); }
public async Task <IActionResult> Pic(int id, IFormFile file) { var user = await uow.User.Get(id); if (user == null) { return(NotFound(new { respose = "error", message = $"user id = {id} does not exist" })); } using (var memStream = new MemoryStream()) { await file.CopyToAsync(memStream); var image = memStream.ToArray(); var mimeType = imageProcessor.GetImageMimeType(image); if (!imageProcessor.SupportedMimeTypes.Contains(mimeType)) { return(BadRequest(new { Result = "error", Message = "unsupported format. the only supported format is JPEG" })); } var blobName = await blobStorage.SaveAsync(image); var media = await uow.Media.Add(new UserMedia { BlobPath = blobName, Extension = imageProcessor.GetImageExtension(image), Uploaded = DateTime.UtcNow, Rel = MediaRelationType.UserPic, UserId = id }); await uow.CommitAsync(); return(Ok(new { PicUrl = Url.ActionUserPic(blobName) })); } }