Exemplo n.º 1
0
        public async Task CreateActorWithOutPhoto()
        {
            var nameDB  = Guid.NewGuid().ToString();
            var context = BuildContext(nameDB);
            var mapper  = ConfigurationAutoMapper();

            var actor = new ActorCreateDTO {
                Name = "Felipe", Birthday = DateTime.Now
            };

            var mock = new Mock <IFilesStore>();

            mock.Setup(x => x.SaveFile(null, null, null, null)).Returns(Task.FromResult("url"));

            var controller = new ActorsController(context, mapper, mock.Object);
            var response   = await controller.Post(actor);

            var result = response as CreatedAtRouteResult;

            Assert.AreEqual(201, result.StatusCode);

            var context2 = BuildContext(nameDB);
            var list     = await context2.Actors.ToListAsync();

            Assert.AreEqual(1, list.Count);
            Assert.IsNull(list[0].Image);

            Assert.AreEqual(0, mock.Invocations.Count);
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Put(int id, [FromForm] ActorCreateDTO actorCreateDTO)
        {
            var actor = await _context.Actors.FirstOrDefaultAsync(x => x.Id == id);

            if (actor == null)
            {
                return(NotFound());
            }
            actor = _mapper.Map(actorCreateDTO, actor);

            if (actorCreateDTO.Image != null)
            {
                using (var memoryStream = new MemoryStream())
                {
                    await actorCreateDTO.Image.CopyToAsync(memoryStream);

                    var content   = memoryStream.ToArray();
                    var extension = Path.GetExtension(actorCreateDTO.Image.FileName);
                    actor.Image = await _filesStore.EditFile(content, extension, _container, actor.Image, actorCreateDTO.Image.ContentType);
                }
            }

            await _context.SaveChangesAsync();

            return(NoContent());
        }
        public async Task <IActionResult> UpdateAsync(int Id, [FromForm] ActorCreateDTO actorCreateDTO)
        {
            if (actorCreateDTO == null)
            {
                return(BadRequest());
            }

            var response = await _actoresApplication.UpdateAsync(Id, actorCreateDTO);

            if (response.IsSuccess)
            {
                return(new CreatedAtRouteResult("obtenerActor", new { Id = response.Data.Id }, response.Data));
            }

            return(BadRequest(response.Message));
        }
        public async Task <Response <ActorDTO> > UpdateAsync(int Id, ActorCreateDTO actorCreateCreateDTO)
        {
            var response = new Response <ActorDTO>();

            try
            {
                var actorDB = await _actoresDomain.ExistAsync(Id);

                if (actorDB != null)
                {
                    actorDB = _mapper.Map(actorCreateCreateDTO, actorDB);

                    if (actorCreateCreateDTO.Foto != null)
                    {
                        using (var memoryStream = new MemoryStream())
                        {
                            await actorCreateCreateDTO.Foto.CopyToAsync(memoryStream);

                            var contenido = memoryStream.ToArray();
                            var extension = Path.GetExtension(actorCreateCreateDTO.Foto.FileName);
                            actorDB.Foto = await _almacenadorArhivos.EditarArchivo(contenido, extension, _contenedor, actorDB.Foto, actorCreateCreateDTO.Foto.ContentType);
                        }
                    }


                    actorDB = await _actoresDomain.UpdateAsync(actorDB);

                    response.Data = _mapper.Map <ActorDTO>(actorDB);
                    if (response.Data != null)
                    {
                        response.IsSuccess = true;
                        response.Message   = "Registro Actualizado!!!";
                    }
                }
                else
                {
                    response.IsSuccess = true;
                    response.Message   = "Registro no existe!!!";
                }
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return(response);
        }
Exemplo n.º 5
0
        public async Task CreateActorWithPhoto()
        {
            var nameDB  = Guid.NewGuid().ToString();
            var context = BuildContext(nameDB);
            var mapper  = ConfigurationAutoMapper();

            var content = Encoding.UTF8.GetBytes("Image Test");
            var file    = new FormFile(new MemoryStream(content), 0, content.Length, "Data", "image.jpg");

            file.Headers     = new HeaderDictionary();
            file.ContentType = "image/jpg";

            var actor = new ActorCreateDTO
            {
                Name     = "New Actor",
                Birthday = DateTime.Now,
                Image    = file
            };

            var mock = new Mock <IFilesStore>();

            mock.Setup(x => x.SaveFile(content, ".jpg", "actors", file.ContentType))
            .Returns(Task.FromResult("url"));

            var controller = new ActorsController(context, mapper, mock.Object);
            var response   = await controller.Post(actor);

            var result = response as CreatedAtRouteResult;

            Assert.AreEqual(201, result.StatusCode);

            var context2 = BuildContext(nameDB);
            var list     = await context2.Actors.ToListAsync();

            Assert.AreEqual(1, list.Count);
            Assert.AreEqual("url", list[0].Image);
            Assert.AreEqual(1, mock.Invocations.Count);
        }
Exemplo n.º 6
0
        public async Task <ActionResult> Post([FromForm] ActorCreateDTO actorCreateDTO)
        {
            var actor = _mapper.Map <Actor>(actorCreateDTO);

            if (actorCreateDTO.Image != null)
            {
                using (var memoryStream = new MemoryStream())
                {
                    await actorCreateDTO.Image.CopyToAsync(memoryStream);

                    var content   = memoryStream.ToArray();
                    var extension = Path.GetExtension(actorCreateDTO.Image.FileName);
                    actor.Image = await _filesStore.SaveFile(content, extension, _container, actorCreateDTO.Image.ContentType);
                }
            }

            _context.Actors.Add(actor);
            await _context.SaveChangesAsync();

            var dto = _mapper.Map <ActorDTO>(actor);

            return(new CreatedAtRouteResult("GetActor", new { id = actor.Id }, dto));
        }