Example #1
0
        public async Task <APIResponse <IEnumerable <Model> > > PostMany([FromBody] IEnumerable <ModelDto> modelDtos)
        {
            // Validate model
            if (!ModelState.IsValid)
            {
                var errors = new List <string>();
                foreach (var state in ModelState)
                {
                    foreach (var error in state.Value.Errors)
                    {
                        errors.Add(error.ErrorMessage);
                    }
                }

                var err = new { errors = errors };

                return(new APIResponse <IEnumerable <Model> >(400, err, null));
            }

            var models = _mapper.Map <IEnumerable <Model> >(modelDtos);

            var modelsAdded = await _modelService.AddManyModelsAsync(models);

            if (modelsAdded != null)
            {
                return(new APIResponse <IEnumerable <Model> >(201, "All Models are created", modelsAdded));
            }
            return(new APIResponse <IEnumerable <Model> >(204, "All Models are not created", modelsAdded));
        }