Exemplo n.º 1
0
        public async Task <UploadResponse> Post()
        {
            UploadResponse response = new UploadResponse();

            if (!_validatorService.ValidateRequest(Request))
            {
                response.Error        = true;
                response.ErrorMessage = "Bestand kan niet worden gevonden";
                return(response);
            }

            var file = Request.Form.Files[0];

            if (!_validatorService.ValidateFileExtension(file))
            {
                response.Error        = true;
                response.ErrorMessage = "Bestand is niet in correct formaat.";
                return(response);
            }

            var fileContent = new StringBuilder();

            using (var reader = new StreamReader(file.OpenReadStream()))
            {
                while (reader.Peek() >= 0)
                {
                    fileContent.AppendLine(reader.ReadLine());
                }
            }

            int lineNumber = _validatorService.ValidateString(fileContent.ToString());

            if (lineNumber > 0)
            {
                response.Error        = true;
                response.ErrorMessage = $"Bestand is niet in correct formaat op regel {lineNumber}.\nEr zijn geen cursusinstanties toegevoegd.";
                return(response);
            }

            List <CourseInstance> courseInstances = _convertService.Convert(fileContent.ToString());

            (response.NewCourses, response.NewInstances, response.Duplicates) = await _insertService.InsertInstances(courseInstances);

            return(response);
        }