public ActionResult <TopicDto> CreateTopic(Guid personId, Guid projectId, [FromBody] CreateTopicDto dto) { try { if (!ModelState.IsValid) { return(BadRequest()); } if (!_db.Person.BelongsToUser(personId, HttpContext)) { return(Forbid()); } if (_db.Participation.GetRole(personId, projectId)?.KnowledgeBaseWrite != true) { return(Forbid()); } var topic = _mapper.Map <Topic>(dto); topic.ProjectId = projectId; _db.Topic.Create(topic); _db.Save(); var insertedTopic = _db.Topic .FindByCondition(x => x.Id == topic.Id) .SingleOrDefault(); return(Ok(_mapper.Map <TopicDto>(insertedTopic))); } catch (Exception e) { _logger.LogError($"ERROR in CreateTopic: {e.Message}"); return(StatusCode(500, "Internal server error")); } }
public IHttpActionResult CreateTopic(CreateTopicDto model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (_topicService.FindByTitle(model.Title) != null) { return(BadRequest("Topic already exists")); } if (_userService.Get(model.ModeratingUserId) == null) { return(BadRequest("User not found.")); } var topic = _topicService.Add(new Topic { Id = Guid.NewGuid(), Title = model.Title, Description = model.Description, ModeratingUserId = model.ModeratingUserId, CreateDateTime = DateTime.Now }); return(Ok(Mapper.Map <BasicTopicDto>(topic))); }
public void CreateTopic(CreateTopicDto newCreateTopicData) { var newTopic = Mapper.Map <Topic>(newCreateTopicData); newTopic.UserId = _accRepo.GetProfileIdByAccountId(newCreateTopicData.UserAccountId); _topicRepo.AddNewItem(newTopic); _topicRepo.SaveChanges(); }
public async Task <IActionResult> AddTopic([FromBody] CreateTopicDto topicDto) { var topic = new Topic { Name = topicDto.Name, Description = topicDto.Description }; var result = await _topicRepo.InsertTopicToDatabaseAsync(topic); if (result) { return(CreatedAtRoute("GetTopicById", new { id = topic.TopicId }, _mapper.Map <TopicDto>(topic))); } return(StatusCode(500, "Unable to Add topic at this time, please try later")); }
public IActionResult Post([FromQuery] CreateTopicDto dto) { try { _addCommand.Execute(dto); return(NoContent()); } catch (EntityAlreadyExistsException) { return(NotFound()); } catch (Exception) { return(StatusCode(500, "An error ocurred")); } }
public void Execute(CreateTopicDto request) { if (Context.Topics.Any(t => t.Subject == request.Subject)) { throw new EntityAlreadyExistsException(); } Context.Topics.Add(new Topic { Subject = request.Subject, Content = request.Content, UserId = request.UserId, CategoryId = request.CategoryId }); Context.SaveChanges(); }
public ActionResult Create(CreateTopicDto dto) { if (!ModelState.IsValid) { return(View(dto)); } try { _addCommand.Execute(dto); return(RedirectToAction(nameof(Index))); } catch (EntityAlreadyExistsException) { TempData["error"] = "with the same name already exists"; } catch (Exception) { TempData["error"] = "An error occured"; } return(View()); }