Exemplo n.º 1
0
        public IActionResult DeleteCustomFileMaterialProp(int materialId, Guid propId)
        {
            try
            {
                // Get custom prop and validate type
                CustomMaterialProp prop = CustomMaterialPropService.GetCustomMaterialProp(propId);
                if (prop.Type != PropType.File)
                {
                    return(HandleBadRequest("The submitted prop is not of the type `file`."));
                }

                // Update material - remove prop
                MaterialsService.UpdateCustomFileMaterialProp(materialId, prop, null);

                // Done!
                return(NoContent());
            }
            catch (CustomPropNotFoundException exception)
            {
                return(HandleResourceNotFoundException(exception));
            }
            catch (MaterialNotFoundException exception)
            {
                return(HandleResourceNotFoundException(exception));
            }
            catch (Exception exception)
            {
                return(HandleUnexpectedException(exception));
            }
        }
Exemplo n.º 2
0
        public IActionResult SetCustomFileMaterialProp(int materialId, Guid propId, IFormFile file)
        {
            if (file.Length <= 0)
            {
                return(HandleBadRequest("No file content found."));
            }

            try
            {
                // Get custom prop and validate type
                CustomMaterialProp prop = CustomMaterialPropService.GetCustomMaterialProp(propId);
                if (prop.Type != PropType.File)
                {
                    return(HandleBadRequest("The submitted prop is not of the type `file`."));
                }
                MaterialsService.UpdateCustomFileMaterialProp(materialId, prop, file);

                // Done!
                return(NoContent());
            }
            catch (CustomPropNotFoundException exception)
            {
                return(HandleResourceNotFoundException(exception));
            }
            catch (MaterialNotFoundException exception)
            {
                return(HandleResourceNotFoundException(exception));
            }
            catch (Exception exception)
            {
                return(HandleUnexpectedException(exception));
            }
        }
        public IActionResult CreateCustomMaterialProp([FromBody] CustomMaterialPropCreationRequest customMaterialPropCreationRequest)
        {
            if (customMaterialPropCreationRequest == null ||
                string.IsNullOrWhiteSpace(customMaterialPropCreationRequest.Name))
            {
                return(HandleBadRequest("Missing prop data: a name and type are required."));
            }

            // Attempt to parse type
            PropType type;

            if (Enum.IsDefined(typeof(PropType), customMaterialPropCreationRequest.Type))
            {
                type = (PropType)customMaterialPropCreationRequest.Type;
            }
            else
            {
                return(HandleBadRequest("A valid prop type needs to be provided."));
            }

            // Attempt to create
            try
            {
                CustomMaterialProp prop = CustomMaterialPropService.CreateCustomMaterialProp(customMaterialPropCreationRequest.Name, type);
                return(Created(GetNewResourceUri(prop.Id), prop));
            }
            catch (Exception exception)
            {
                return(HandleUnexpectedException(exception));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the file associated with a custom material property - or null if the file could not be found.
        /// </summary>
        /// <param name="id">The ID of the material to get a file for.</param>
        /// <param name="prop">The custom prop to get the file from.</param>
        /// <returns>Returns the file path or null</returns>
        public string GetCustomPropFilePath(int id, CustomMaterialProp prop)
        {
            // Make sure the material exists
            Material material = GetMaterialOrThrowNotFoundException(id);

            // Get and validate target directory
            string basePath = Path.Combine(Confguration.GetValue <string>("Files:Path"), id.ToString());

            if (!Directory.Exists(basePath))
            {
                Directory.CreateDirectory(basePath);
                return(null);
            }

            // Get file path
            CustomMaterialPropValue propValue = material.CustomProps.FirstOrDefault(p => p.PropId == prop.Id);
            string filePath = (string)propValue.Value;

            // Validate file and return path (or null)
            if (File.Exists(filePath))
            {
                return(filePath);
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// Updates a custom material property.
        /// </summary>
        /// <param name="name">The new name to set.</param>
        /// <returns>Returns the updated prop.</returns>
        /// <exception cref="CustomPropNotFoundException">Thrown if no matching prop could be found.</exception>
        public CustomMaterialProp UpdateCustomMaterialProp(Guid id, string name)
        {
            CustomMaterialProp prop = GetCustomMaterialPropOrThrowNotFoundException(id);

            prop.Name = name;
            PropRepository.UpdateCustomMaterialProp(prop);
            return(prop);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets a custom material property.
        /// </summary>
        /// <param name="id">The property's ID.</param>
        /// <returns>
        /// Returns the property or null.
        /// </returns>
        public CustomMaterialProp GetCustomMaterialProp(Guid id)
        {
            CustomMaterialProp prop = null;

            using (IDbConnection connection = GetNewConnection())
            {
                prop = connection.QuerySingle <CustomMaterialProp>("SELECT * FROM material_props WHERE id=@Id", new { id });
            }
            return(prop);
        }
        /// <summary>
        /// Attempts to get a material prop from the underlying repository and throws a <see cref="CustomPropNotFoundException"/> if no matching prop could be found.
        /// </summary>
        /// <param name="id">ID of the prop to get.</param>
        /// <exception cref="CustomPropNotFoundException">Thrown if no matching prop could be found.</exception>
        /// <returns>Returns the prop, if found.</returns>
        private CustomMaterialProp GetCustomMaterialPropOrThrowNotFoundException(Guid id)
        {
            CustomMaterialProp prop = PropRepository.GetCustomMaterialProp(id);

            // Check for prop existence
            if (prop == null)
            {
                throw new CustomPropNotFoundException(id);
            }

            return(prop);
        }
        public CustomMaterialProp CreateCustomMaterialProp(string name, PropType type)
        {
            CustomMaterialProp prop = new CustomMaterialProp()
            {
                Id   = Guid.NewGuid(),
                Name = name,
                Type = type
            };

            Props.Add(prop.Id, prop);
            return(prop);
        }
Exemplo n.º 9
0
 /// <summary>
 /// Updates a custom material property.
 /// </summary>
 /// <param name="prop">Prop to update.</param>
 public void UpdateCustomMaterialProp(CustomMaterialProp prop)
 {
     using (IDbConnection connection = GetNewConnection())
     {
         connection.Execute("UPDATE material_props SET name=@Name, type=@Type WHERE id=@Id",
                            param: new
         {
             prop.Id,
             prop.Name,
             prop.Type
         });
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// Sets or removes a custom material prop value of the text type.
        /// </summary>
        /// <param name="id">The material ID.</param>
        /// <param name="prop">The custom prop ID.</param>
        /// <param name="text">The text to set, or null.</param>
        public void UpdateCustomTextMaterialProp(int id, CustomMaterialProp prop, string text)
        {
            // Make sure the material exists
            GetMaterialOrThrowNotFoundException(id);

            // Proceed
            if (text == null)
            {
                CustomMaterialPropValueRepository.RemoveCustomTextMaterialProp(id, prop.Id);
            }
            else
            {
                CustomMaterialPropValueRepository.SetCustomTextMaterialProp(id, prop.Id, text);
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Creates a custom material property.
        /// </summary>
        /// <param name="name">The property's name.</param>
        /// <param name="type">The property's type.</param>
        /// <returns>
        /// Returns the newly created prop.
        /// </returns>
        public CustomMaterialProp CreateCustomMaterialProp(string name, PropType type)
        {
            Guid id = Guid.NewGuid();

            CustomMaterialProp prop = null;

            using (IDbConnection connection = GetNewConnection())
            {
                connection.Execute("INSERT INTO material_props (id, name, type) VALUES (@Id, @Name, @Type)",
                                   param: new
                {
                    id,
                    name,
                    type
                });
                prop = connection.QuerySingle <CustomMaterialProp>("SELECT * FROM material_props WHERE id=@Id", param: new { id });
            }
            return(prop);
        }
        public IActionResult UpdateCustomMaterialProp(Guid id, [FromBody] CustomMaterialPropUpdateRequest customMaterialPropUpdateRequest)
        {
            if (customMaterialPropUpdateRequest == null ||
                string.IsNullOrWhiteSpace(customMaterialPropUpdateRequest.Name))
            {
                return(HandleBadRequest("A valid prop name needs tu be supplied."));
            }

            try
            {
                CustomMaterialProp prop = CustomMaterialPropService.UpdateCustomMaterialProp(id, customMaterialPropUpdateRequest.Name);
                return(Ok(prop));
            }
            catch (CustomPropNotFoundException exception)
            {
                return(HandleResourceNotFoundException(exception));
            }
            catch (Exception exception)
            {
                return(HandleUnexpectedException(exception));
            }
        }
Exemplo n.º 13
0
        public void TestCustomMaterialPropRepository()
        {
            Assert.Empty(Repository.GetAllCustomMaterialProps());

            // Create props
            CustomMaterialProp prop1 = Repository.CreateCustomMaterialProp("Text", PropType.Text);
            CustomMaterialProp prop2 = Repository.CreateCustomMaterialProp("File", PropType.File);

            Assert.Equal(2, Repository.GetAllCustomMaterialProps().Count());
            Assert.Equal("Text", prop1.Name);
            Assert.Equal("File", prop2.Name);
            Assert.Equal(PropType.Text, prop1.Type);
            Assert.Equal(PropType.File, prop2.Type);

            // Get single prop
            prop1 = Repository.GetCustomMaterialProp(prop1.Id);
            Assert.Equal("Text", prop1.Name);
            Assert.Equal(PropType.Text, prop1.Type);

            // Update prop
            prop1.Name = "Datenblatt";
            prop1.Type = PropType.File;
            Repository.UpdateCustomMaterialProp(prop1);
            prop1 = Repository.GetCustomMaterialProp(prop1.Id);
            Assert.Equal("Datenblatt", prop1.Name);
            Assert.Equal(PropType.File, prop1.Type);

            // Delete prop
            Repository.DeleteCustomMaterialProp(prop1.Id);
            IEnumerable <CustomMaterialProp> props = Repository.GetAllCustomMaterialProps();

            Assert.Single(props);
            CustomMaterialProp prop = props.Single();

            Assert.Equal("File", prop.Name);
            Assert.Equal(PropType.File, prop.Type);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Sets or removes a custom material prop value of the file type.
        /// This stores the file in the file system and persists the file path as a custom material prop value.
        /// </summary>
        /// <param name="id">The material ID.</param>
        /// <param name="prop">The custom prop ID.</param>
        /// <param name="file">The file to set, or null.</param>
        public void UpdateCustomFileMaterialProp(int id, CustomMaterialProp prop, IFormFile file)
        {
            // Make sure the material exists
            Material material = GetMaterialOrThrowNotFoundException(id);

            // Get and validate target directory
            string basePath = Path.Combine(Confguration.GetValue <string>("Files:Path"), id.ToString());

            if (!Directory.Exists(basePath))
            {
                Directory.CreateDirectory(basePath);
            }

            if (file == null)
            {
                // Get file path
                CustomMaterialPropValue propValue = material.CustomProps.FirstOrDefault(p => p.PropId == prop.Id);
                string filePath = (string)propValue.Value;

                // Delete file
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
                CustomMaterialPropValueRepository.RemoveCustomFileMaterialProp(id, prop.Id);
            }
            else
            {
                // Write file
                string filePath = Path.Combine(basePath, file.FileName);
                using (FileStream stream = File.Create(filePath))
                {
                    file.CopyTo(stream);
                }
                CustomMaterialPropValueRepository.SetCustomFileMaterialProp(id, prop.Id, filePath);
            }
        }
Exemplo n.º 15
0
        public IActionResult DownloadCustomPropFile(int materialId, Guid propId)
        {
            try
            {
                // Get custom prop and validate type
                CustomMaterialProp prop = CustomMaterialPropService.GetCustomMaterialProp(propId);
                if (prop.Type != PropType.File)
                {
                    return(HandleBadRequest("The submitted prop is not of the type `file`."));
                }

                // Get and validate file path
                string path = MaterialsService.GetCustomPropFilePath(materialId, prop);
                if (string.IsNullOrWhiteSpace(path))
                {
                    return(NotFound(new ClientErrorResponse("File could not be found!")));
                }

                string fileName    = Path.GetFileName(path);
                byte[] fileBytes   = System.IO.File.ReadAllBytes(path);
                string contentType = "application/octet-stream";
                new FileExtensionContentTypeProvider().TryGetContentType(fileName, out contentType);
                return(File(fileBytes, contentType, fileName));
            }
            catch (CustomPropNotFoundException exception)
            {
                return(HandleResourceNotFoundException(exception));
            }
            catch (MaterialNotFoundException exception)
            {
                return(HandleResourceNotFoundException(exception));
            }
            catch (Exception exception)
            {
                return(HandleUnexpectedException(exception));
            }
        }
Exemplo n.º 16
0
        public IActionResult SetCustomTextMaterialProp(int materialId, Guid propId, [FromBody] SetCustomTextMaterialPropRequest setCustomTextMaterialPropRequest)
        {
            if (setCustomTextMaterialPropRequest == null ||
                string.IsNullOrWhiteSpace(setCustomTextMaterialPropRequest.Text))
            {
                return(HandleBadRequest("A text to set for the custom material prop has to be provided."));
            }

            try
            {
                // Get custom prop and validate type
                CustomMaterialProp prop = CustomMaterialPropService.GetCustomMaterialProp(propId);
                if (prop.Type != PropType.Text)
                {
                    return(HandleBadRequest("The submitted prop is not of the type `text`."));
                }

                // Update material - set prop
                MaterialsService.UpdateCustomTextMaterialProp(materialId, prop, setCustomTextMaterialPropRequest.Text);

                // Done!
                return(NoContent());
            }
            catch (CustomPropNotFoundException exception)
            {
                return(HandleResourceNotFoundException(exception));
            }
            catch (MaterialNotFoundException exception)
            {
                return(HandleResourceNotFoundException(exception));
            }
            catch (Exception exception)
            {
                return(HandleUnexpectedException(exception));
            }
        }
Exemplo n.º 17
0
        public void TestCustomMaterialPropValues()
        {
            // Create materials
            Material m1 = Repository.CreateMaterial("m1", "Alpha", "alpha-m1", PC);
            Material m2 = Repository.CreateMaterial("m2", "Alpha", "alpha-m2", PE);
            Material m3 = Repository.CreateMaterial("m3", "Beta", "beta-m1", PC);

            // Create custom props
            CustomMaterialProp textProp = PropRepository.CreateCustomMaterialProp("test", PropType.Text);
            CustomMaterialProp fileProp = PropRepository.CreateCustomMaterialProp("file", PropType.Text);

            // Test getters with no prop values
            IEnumerable <Material> materials = Repository.GetAllMaterials();

            Assert.Equal(3, materials.Count());
            Assert.Empty(materials.Where(m => m.CustomProps.Count > 0));

            // Set custom prop values
            PropValueRepository.SetCustomTextMaterialProp(m1.Id, textProp.Id, "Lorem Ipsum");
            PropValueRepository.SetCustomFileMaterialProp(m1.Id, fileProp.Id, "Z:/my-files/file.pdf");
            PropValueRepository.SetCustomFileMaterialProp(m2.Id, fileProp.Id, "X:/formatc.cmd");

            // Test custom prop values
            materials = Repository.GetAllMaterials();
            m1        = materials.Single(m => m.Name == "m1");
            m2        = materials.Single(m => m.Name == "m2");
            m3        = materials.Single(m => m.Name == "m3");
            Assert.Equal(2, m1.CustomProps.Count);
            Assert.Single(m2.CustomProps);
            Assert.Empty(m3.CustomProps);
            Assert.Single(m1.CustomProps.Where(p => p.PropId == textProp.Id && (string)p.Value == "Lorem Ipsum"));
            Assert.Single(m1.CustomProps.Where(p => p.PropId == fileProp.Id && (string)p.Value == "Z:/my-files/file.pdf"));
            Assert.Single(m2.CustomProps.Where(p => p.PropId == fileProp.Id && (string)p.Value == "X:/formatc.cmd"));

            // Overwrite custom prop value
            PropValueRepository.SetCustomTextMaterialProp(m1.Id, textProp.Id, "Foo Bar");
            m1 = Repository.GetMaterial(m1.Id);
            Assert.Equal(2, m1.CustomProps.Count);
            Assert.Empty(m1.CustomProps.Where(p => p.PropId == textProp.Id && (string)p.Value == "Lorem Ipsum"));
            Assert.Single(m1.CustomProps.Where(p => p.PropId == textProp.Id && (string)p.Value == "Foo Bar"));
            Assert.Single(m1.CustomProps.Where(p => p.PropId == fileProp.Id && (string)p.Value == "Z:/my-files/file.pdf"));

            // Add second text prop
            CustomMaterialProp textProp2 = PropRepository.CreateCustomMaterialProp("troll", PropType.Text);

            PropValueRepository.SetCustomTextMaterialProp(m1.Id, textProp2.Id, "Ak Bars");
            m1 = Repository.GetMaterial(m1.Id);
            Assert.Equal(3, m1.CustomProps.Count);
            Assert.Single(m1.CustomProps.Where(p => p.PropId == textProp.Id && (string)p.Value == "Foo Bar"));
            Assert.Single(m1.CustomProps.Where(p => p.PropId == textProp2.Id && (string)p.Value == "Ak Bars"));
            Assert.Single(m1.CustomProps.Where(p => p.PropId == fileProp.Id && (string)p.Value == "Z:/my-files/file.pdf"));

            // Add second file prop
            CustomMaterialProp fileProp2 = PropRepository.CreateCustomMaterialProp("super-file", PropType.Text);

            PropValueRepository.SetCustomTextMaterialProp(m2.Id, fileProp2.Id, "F:/ile/path");
            m2 = Repository.GetMaterial(m2.Id);
            Assert.Equal(2, m2.CustomProps.Count);
            Assert.Single(m2.CustomProps.Where(p => p.PropId == fileProp.Id && (string)p.Value == "X:/formatc.cmd"));
            Assert.Single(m2.CustomProps.Where(p => p.PropId == fileProp2.Id && (string)p.Value == "F:/ile/path"));

            // Check getAll again
            materials = Repository.GetAllMaterials();
            m1        = materials.Single(m => m.Name == "m1");
            m2        = materials.Single(m => m.Name == "m2");
            m3        = materials.Single(m => m.Name == "m3");
            Assert.Equal(3, m1.CustomProps.Count);
            Assert.Equal(2, m2.CustomProps.Count);
            Assert.Empty(m3.CustomProps);

            // Check getter with filters
            materials = Repository.GetFilteredMaterials(null, "alpha", PE);
            Assert.Single(materials);
            m2 = materials.Single();
            Assert.Equal(2, m2.CustomProps.Count);
            Assert.Single(m2.CustomProps.Where(p => p.PropId == fileProp.Id && (string)p.Value == "X:/formatc.cmd"));
            Assert.Single(m2.CustomProps.Where(p => p.PropId == fileProp2.Id && (string)p.Value == "F:/ile/path"));

            // Test removal of prop values
            PropValueRepository.RemoveCustomTextMaterialProp(m1.Id, textProp.Id);
            PropValueRepository.RemoveCustomFileMaterialProp(m2.Id, fileProp.Id);
            m1 = Repository.GetMaterial(m1.Id);
            m2 = Repository.GetMaterial(m2.Id);
            Assert.Equal(2, m1.CustomProps.Count);
            Assert.Single(m1.CustomProps.Where(p => p.PropId == textProp2.Id && (string)p.Value == "Ak Bars"));
            Assert.Single(m1.CustomProps.Where(p => p.PropId == fileProp.Id && (string)p.Value == "Z:/my-files/file.pdf"));
            Assert.Single(m2.CustomProps);
            Assert.Single(m2.CustomProps.Where(p => p.PropId == fileProp2.Id && (string)p.Value == "F:/ile/path"));
        }
 public void UpdateCustomMaterialProp(CustomMaterialProp prop)
 {
     Props[prop.Id] = prop;
 }