Ejemplo n.º 1
0
        public IActionResult Edit(EditFriendModel model)
        {
            if (ModelState.IsValid)
            {
                //Obtenems the data our Friend from BBDD
                Friend friend = _friendStore.getFriendData(model.Id);
                //Updating the data our object from model
                friend.Name  = model.Name;
                friend.Email = model.Email;
                friend.City  = model.City;

                if (model.Photo != null)
                {
                    if (model.routePhotoLast != null)
                    {
                        string route = Path.Combine(_hosting.WebRootPath, "images", model.routePhotoLast);
                        System.IO.File.Delete(route);
                    }
                    //SAve the Photo in wwwroot/images
                    friend.routePhoto = UploadImagen(model);
                }

                Friend friendModified = _friendStore.modify(friend);

                return(RedirectToAction("index"));
            }
            return(View(model));
        }
Ejemplo n.º 2
0
        public ViewResult Edit(int id)
        {
            Friend          friend     = _friendStore.getFriendData(id);
            EditFriendModel friendEdit = new EditFriendModel
            {
                Id             = friend.Id,
                Name           = friend.Name,
                Email          = friend.Email,
                City           = friend.City,
                routePhotoLast = friend.routePhoto
            };

            return(View(friendEdit));
        }
Ejemplo n.º 3
0
        private string UploadImagen(EditFriendModel model)
        {
            string nameFile = null;

            if (model.Photo != null)
            {
                string folderUpladed = Path.Combine(_hosting.WebRootPath, "images");
                nameFile = Guid.NewGuid().ToString() + "_" + model.Photo.FileName;
                string route = Path.Combine(folderUpladed, nameFile);
                using (var fileStream = new FileStream(route, FileMode.Create))
                {
                    model.Photo.CopyTo(fileStream);
                }
            }
            return(nameFile);
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Update([FromBody] EditFriendModel friend)
        {
            try
            {
                var friendToUpdate = _repoWrapper.Friends.GetById(friend.Id);
                if (friendToUpdate == null)
                {
                    _logger.LogError("Error inside FriendController Update action: unable to find friend with matching Id");
                    return(StatusCode(500, "Internal Sever Error"));
                }
                if (!String.IsNullOrWhiteSpace(friend.Name))
                {
                    friendToUpdate.Name = friend.Name;
                }
                if (friend.LocationId != null)
                {
                    var location = _repoWrapper.Locations.GetById((int)friend.LocationId);
                    if (location == null)
                    {
                        _logger.LogError("Error inside FriendController Update action: unable to find location with matching Id");
                        return(StatusCode(500, "Internal Sever Error"));
                    }
                    friendToUpdate.LocationId = (int)friend.LocationId;
                }

                _repoWrapper.Friends.Update(friendToUpdate);
                _repoWrapper.UnitOfWorkComplete();
                return(Ok(new
                {
                    success = true,
                    returncode = "200"
                }));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error inside FriendController Update action: {ex.Message}");
                return(StatusCode(500, "Internal Sever Error"));
            }
        }