Esempio n. 1
0
        // ============ Methods to UPDATE something ============

        /**
         * Method that will update the material (name, description and price) with the passed reference OR return all the errors found when trying to do so.
         *
         * Validations performed:
         * 1. Validation of the passed material's reference (database);
         * 2. Validation of the name, description and price of the passed DTO.
         */
        public ValidationOutput Update(string reference, MaterialDto dto)
        {
            //1.
            ValidationOutput validationOutput = new ValidationOutputNotFound();

            if (!MaterialExists(reference))
            {
                validationOutput.AddError("Reference of material",
                                          "No material with the reference '" + reference + "' exists in the system.");
                return(validationOutput);
            }

            validationOutput = new ValidationOutputForbidden();
            if (dto.Reference != null && !dto.Reference.Equals(reference))
            {
                validationOutput.AddError("Reference of material", "It's not allowed to update reference.");
                return(validationOutput);
            }

            //2.
            validationOutput = _materialDTOValidator.DTOIsValidForUpdate(dto);
            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            Material materialToUpdate = _materialRepository.GetByReference(reference);

            if (dto.Name != null)
            {
                materialToUpdate.Name = dto.Name;
            }

            if (dto.Description != null)
            {
                materialToUpdate.Description = dto.Description;
            }

            /*if (dto.Price != null)
             * {
             *  materialToUpdate.AddPriceToHistory(new PriceHistory(_mapper.Map<Price>(dto.Price)));
             * }*/

            validationOutput.DesiredReturn =
                _mapper.Map <MaterialDto>(_materialRepository.Update(materialToUpdate));
            return(validationOutput);
        }
        // ============ Methods to UPDATE something ============

        /**
         * Method that will update the category (name and description), with the passed reference, with the data present in the passed DTO OR return all the errors found when trying to do so.
         *
         * Validations performed:
         * 1. Validation of the passed category's reference (database);
         * 2. Validation of the name and description of the passed DTO.
         */
        public ValidationOutput Update(string refer, CategoryDto dto)
        {
            //1.
            ValidationOutput validationOutput = new ValidationOutputNotFound();

            if (!Exists(refer))
            {
                validationOutput.AddError("Reference of category to update",
                                          "No category with the reference '" + refer + "' exists in the system.");
                return(validationOutput);
            }

            validationOutput = new ValidationOutputForbidden();
            if (dto.Reference != null && !dto.Reference.Equals(refer))
            {
                validationOutput.AddError("Reference of category", "It's not allowed to update reference.");
                return(validationOutput);
            }

            //2.
            validationOutput = _categoryDTOValidator.DTOIsValidForUpdate(dto);
            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            Category categoryToUpdate = _categoryRepository.GetByReference(refer);

            if (dto.Name != null)
            {
                categoryToUpdate.Name = dto.Name;
            }

            if (dto.Description != null)
            {
                categoryToUpdate.Description = dto.Description;
            }

            validationOutput.DesiredReturn = _mapper.Map <CategoryDto>(_categoryRepository.Update(categoryToUpdate));
            return(validationOutput);
        }