public async Task InvokeAsync(HttpContext context, ITopicsRepository topicsRepository, IMapper mapper, DefaultEventGridEventHandler eventHandler, IOptions <AwesomeEventGridOptions> options) { try { ModelState.Reset(); using (var reader = new StreamReader(context.Request.Body, Encoding.UTF8)) { var topicToCreate = JsonConvert.DeserializeObject <TopicModel>(await reader.ReadToEndAsync(), options.Value.SerializerSettings); if (topicToCreate == null) { ModelState.AddError("", "Request body is required"); } Validate(topicToCreate); if (!ModelState.IsValid) { await BadRequest(context); return; } if (topicsRepository.FindByName(topicToCreate.Name) != null) { ModelState.AddError("name", "Topic does already exists"); await BadRequest(context); return; } var topic = mapper.Map <Topic>(topicToCreate); topic = topicsRepository.Add(topic); var topicModel = mapper.Map <TopicModel>(topic); //todo fix url: await CreatedAt(context, "http://foo", topicModel); } } catch (JsonException ex) { ModelState.AddError("", ex.Message); await BadRequest(context); } }
public void Create(Core.CoreModels.Topic topic) { if (topic is null) { throw new ArgumentNullException(nameof(topic)); } var isInvalid = string.IsNullOrWhiteSpace(topic.Id.ToString()) || string.IsNullOrWhiteSpace(topic.Theme) || topic.Id < 0; if (isInvalid) { throw new ArgumentException(TOPIC_IS_INVALID); } _topicsRepository.Add(topic); }