Ejemplo n.º 1
0
        // Edytuje nazwę pliku ze zdjęciem.
        public StatusResult <EditPhotoNameStatus> EditPhotoName(int id, PhotoNameDto dto)
        {
            var toEdit = context.Photos
                         .SingleOrDefault(x => x.Id == id);

            if (toEdit == null)
            {
                return(new StatusResult <EditPhotoNameStatus>
                {
                    Status = EditPhotoNameStatus.BadId
                });
            }

            var directoryName    = hostingEnvironment.WebRootPath + "/media";
            var isFilenameUnique = !System.IO.Directory.GetFiles(directoryName).Select(x => x.Split(Path.DirectorySeparatorChar).Last().Split('.').First()).Any(x => x == dto.Name);

            if (!isFilenameUnique)
            {
                return(new StatusResult <EditPhotoNameStatus>
                {
                    Status = EditPhotoNameStatus.Conflict
                });
            }

            System.IO.File.Move($"{hostingEnvironment.WebRootPath}{toEdit.Url}", $"{hostingEnvironment.WebRootPath}/media/{dto.Name}.{toEdit.Url.Split('.').Last()}");
            toEdit.Url = $"/media/{dto.Name}.{toEdit.Url.Split('.').Last()}";
            context.SaveChanges();

            return(new StatusResult <EditPhotoNameStatus>
            {
                Status = EditPhotoNameStatus.Ok
            });
        }
Ejemplo n.º 2
0
        public IActionResult EditPhotoName(int id, [FromBody] PhotoNameDto dto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var editStatus = photoService.EditPhotoName(id, dto).Status;

            switch (editStatus)
            {
            case EditPhotoNameStatus.BadId:
                return(NotFound());

            case EditPhotoNameStatus.Conflict:
                return(StatusCode(409));

            case EditPhotoNameStatus.Ok:
                return(NoContent());

            default:
                return(StatusCode(500));
            }
        }