Beispiel #1
0
        public async Task <IActionResult> AgregarFotoAUsuario(int id, [FromForm] PhotoACrearDTO photoDTO)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var usuario = await repo.GetUsuario(id);

            var archivo = photoDTO.Archivo;

            // UTILIZANDO LA LIBRERIA DE CLOUDINARY
            var resultado = new ImageUploadResult();

            if (archivo.Length > 0)
            {
                using (var stream = archivo.OpenReadStream())
                {
                    // File es el nombre del atributo de la clase ImageUploadParams (Propio de Cloudinary)
                    var parametrosDeSubida = new ImageUploadParams()
                    {
                        File           = new FileDescription(archivo.Name, stream),
                        Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face")
                    };
                    resultado = cloudinary.Upload(parametrosDeSubida);
                }
            }

            photoDTO.Url      = resultado.Uri.ToString();
            photoDTO.PublicId = resultado.PublicId;

            var photo = mapper.Map <Photo>(photoDTO);

            if (!usuario.FotosPublicas.Any(u => u.EsPrincipal))
            {
                photo.EsPrincipal = true;
            }

            usuario.FotosPublicas.Add(photo);

            if (await repo.GuardarCambios())
            {
                var photoADevolver = mapper.Map <PhotoADevolverDTO>(photo);
                return(CreatedAtRoute("ObtenerPhoto", new { id = usuario.Id, idPhoto = photo.id }, photoADevolver));
                // id es el parametro de id de usuario que requiere la ruta, idPhoto es la que requiere ObtenerPhoto
            }
            return(BadRequest("No se pudo agregar la foto."));
        }
Beispiel #2
0
        public async Task <IActionResult> EditarUsuario(int id, UsuarioAEditarDTO usuarioEdit)
        {
            // Verifico que el usuario que esta intentando actualizar su perfil coincide con el perfil a editar
            // Lo que hace esta linea es obtener el NameIdentifier del token del usuario, convertirlo en int y compararlo con el id del parametro.

            // This ClaimType comes from the HttpRequest context, which has access to the token that was sent up by the client to the API.    From here we can get the user id so it will always match with the currently logged on user.

            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var usuario = await Db.GetUsuario(id);

            Mapper.Map(usuarioEdit, usuario);

            if (await Db.GuardarCambios())
            {
                return(NoContent());
            }
            throw new Exception("Ha ocurrido un error al actualizar los datos del usuario id:" + id);
        }