Ejemplo n.º 1
0
        public async Task <IActionResult> UploadDataByDatasetIdAsync(long id, IFormFile file)
        {
            if (file == null)
            {
                return(BadRequest(ResponseResult.Failed(ErrorCode.ValidationError, "File can't be empty.")));
            }

            var dataset = await _datasetRepository.GetDatasetByIdAsync(id);

            if (dataset == null)
            {
                return(NotFound(ResponseResult.Failed(ErrorCode.Error, "Dataset isn't found.")));
            }

            if (dataset.IsImported)
            {
                return(BadRequest(ResponseResult.Failed(ErrorCode.Error, "Dataset can't be imported more than once.")));
            }

            var friendships = new List <Friendship>();

            using (StreamReader streamReader = new StreamReader(file.OpenReadStream()))
            {
                string line;
                while ((line = streamReader.ReadLine()) != null)
                {
                    var fields = line.Split(' ');
                    friendships.Add(Friendship.New(long.Parse(fields[0]), long.Parse(fields[1]), dataset));
                }
            }

            await _friendShipRepository.AddDataAsync(friendships);

            dataset.Update();

            await _datasetRepository.UpdateDatasetAsync(dataset);

            return(Ok(ResponseResult.Succeeded()));
        }