public void UpdateForum(ForumDTO updatedForum) { if (_forumRepository.GetForumByID(updatedForum.ForumID) == null) { throw new NotFoundException("Forum with that ID does not exist!"); } var oldForum = _forumRepository.GetForumByID(updatedForum.ForumID); var newForum = mapper.Map <Forum>(updatedForum); if (oldForum.ForumID != newForum.ForumID) { throw new ErrorOccurException("Forum ID can not be changed!"); } try { newForum.UserID = oldForum.UserID; mapper.Map(newForum, oldForum); _forumRepository.SaveChanges(); } catch (Exception ex) { throw new ErrorOccurException("Error updating forum: " + ex.Message); } }
public async Task <IHttpActionResult> CreateNew([FromBody] ForumDTO data) { var validator = new ForumCreateNewValidator(); var validationResult = await validator.ValidateAsync(data); if (!validationResult.IsValid) { foreach (var error in validationResult.Errors) { ModelState.AddModelError(error.PropertyName, error.ErrorMessage); } return(BadRequest(ModelState)); } var forum = new Forum { ParentId = data.ParentId, Name = data.Name, Created = DateTime.Now }; var storedForum = Db.Forums.Add(forum); await Db.SaveChangesAsync(); return(Ok(storedForum.Id)); }
public long Insert(Forum forum) { ForumDTO dto = new ForumDTO(); dto = converter.ModelToDTO(forum); return(context.Insert(dto)); }
public void CreateForum(string name, string description) { ForumDTO forumdto = new ForumDTO(id, name, description); id += 1; forumdata.Add(forumdto); }
public HttpResponseMessage GetForumById(int id) { try { Forums forum = this._forumManagement.GetForumById(id); if (forum != null) { ForumDTO dto = new ForumDTO { ForumId = forum.ForumId, Category = forum.Category, OwnerId = forum.OwnerId }; return(Request.CreateResponse <ForumDTO>(HttpStatusCode.OK, dto)); } else { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Resource Not Found")); } } catch (Exception) { // Log exception code goes here return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error occured while executing method.")); } }
public async Task <IHttpActionResult> Edit(int id, [FromBody] ForumDTO data) { var validator = new ForumEditValidator(); var validationResult = await validator.ValidateAsync(data); if (!validationResult.IsValid) { foreach (var error in validationResult.Errors) { ModelState.AddModelError(error.PropertyName, error.ErrorMessage); } return(BadRequest(ModelState)); } var forum = await Db.Forums.FindAsync(id); if (forum == null) { return(NotFound()); } forum.ParentId = data.ParentId; forum.Name = data.Name; Db.Forums.Attach(forum); var entry = Db.Entry <Forum>(forum); entry.State = EntityState.Modified; await Db.SaveChangesAsync(); return(StatusCode(HttpStatusCode.NoContent)); }
public static ForumDTO Map(ForumModel forumModel) { ForumDTO result = MapperBag.ForumMapper.Map(forumModel); result.TopicDtos = MapperBag.TopicMapper.Map(forumModel.Topics).ToList(); return(result); }
//This forum gets single forum information public ForumDTO GetForumById(int id) { ForumDTO forumdto = new ForumDTO(); try { using (conn = new MySqlConnection(Connectionstring.Connstring)) { conn.Open(); string query = "SELECT * from forum WHERE id = @id"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@id", id); var reader = cmd.ExecuteReader(); while (reader.Read()) { int Id = reader.GetInt32(0); string Name = reader.GetString(1); string Description = reader.GetString(4); forumdto = new ForumDTO(Id, Name, Description); } } } } catch (NullReferenceException) { forumdto = null; } return(forumdto); }
public OperationDetails Create(ForumDTO forumDto) { if (forumDto == null) { return(new OperationDetails(false, "Empty subforum has been provided", "")); } if (forumDto.Name == null || forumDto.Name == string.Empty) { return(new OperationDetails(false, "Subforum name required", "Name")); } try { var forum = MapperBag.ForumMapper.Map(forumDto); if (unitOfWork.ForumRepository.Find(f => f.Name == forum.Name).FirstOrDefault() != null) { return(new OperationDetails(false, "Subforum with the given name already exists", "Name")); } unitOfWork.ForumRepository.Create(forum); unitOfWork.Save(); return(new OperationDetails(true, "Subforum was successfully created", "")); } catch (Exception ex) { return(new OperationDetails(false, ex.Message, "")); } }
//GetAllForums out of the DataBase public List <ForumDTO> GetAllForums() { List <ForumDTO> forumdtos = new List <ForumDTO>(); try { using (conn = new MySqlConnection(Connectionstring.Connstring)) { string query = "SELECT * FROM `forum` LEFT JOIN post ON forum.Id = post.forum ORDER BY forum.Name"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) { conn.Open(); var reader = cmd.ExecuteReader(); while (reader.Read()) { int Id = reader.GetInt32(0); string Name = reader.GetString(1); string Description = reader.GetString(4); ForumDTO forumdto = new ForumDTO(Id, Name, Description); forumdtos.Add(forumdto); } } } } catch (NullReferenceException) { forumdtos = null; } return(forumdtos); }
public void ForumGetByIdTest(int id, string title, string description, int creatorid) { ForumDTO dto = new ForumDTO(id, title, description, creatorid); ForumDTO result = converter.ModelToDTO(container.GetById(id)); dto.Should().BeEquivalentTo(result); }
public Forum(ForumDTO forumdto, List <Post> posts, List <User> users) { Id = forumdto.Id; ForumTitel = forumdto.Name; Desciption = forumdto.Description; Posts = posts; Users = users; }
public Forum GetById(int id) { ForumDTO dto = new ForumDTO(); dto = context.GetById(id); Forum forum = converter.DtoToModel(dto); return(forum); }
public async Task <Forum> CreateForumTheme(ForumDTO forum) { var mappedForum = _mapper.Map <Forum>(forum); var result = this._context.Forums.Add(mappedForum); await this._context.SaveChangesAsync(); return(result.Entity); }
public long Insert(ForumDTO dto) { if (tests == null) { throw new NullReferenceException("No value returned"); } tests.Add(dto); return(tests[0].Id); }
public Forum GetForumById(int id) { ForumDTO forumdto = new ForumDTO(); forumdto = forumrepository.GetForumById(id); postContainer.GetAllPostsByForumId(forumdto.Id); userContainer.GetAllUsersByForumId(forumdto.Id); Forum forum = new Forum(forumdto, postContainer.Posts, userContainer.Users); return(forum); }
public List <ForumDTO> GetAll() { ForumDTO dto = new ForumDTO(1, "General", "Description", 1); tests.Add(dto); if (tests == null) { throw new NullReferenceException("No value returned"); } return(tests); }
public ForumInMemory() { ForumDTO forumdto = new ForumDTO(1, "test1", "test1"); ForumDTO forumdto2 = new ForumDTO(2, "test2", "test2"); ForumDTO forumdto3 = new ForumDTO(3, "test3", "test3"); ForumDTO forumdto4 = new ForumDTO(4, "test4", "test4"); forumdata.Add(forumdto); forumdata.Add(forumdto2); forumdata.Add(forumdto3); forumdata.Add(forumdto4); }
public async Task <IActionResult> CreateForumTheme(ForumDTO forumDTO) { var forum = await _forumService.CreateForumTheme(forumDTO); if (forum == null) { return(BadRequest("Something went wrong :(")); } else { return(Ok("The forum theme was created!")); } }
public ForumDTO CreateForum(ForumDTO newForum) { Forum entity = mapper.Map <Forum>(newForum); try { var forum = _forumRepository.CreateForum(entity); _forumRepository.SaveChanges(); return(mapper.Map <ForumDTO>(forum)); } catch (Exception ex) { throw new ErrorOccurException(ex.Message); } }
public ForumDTO GetById(int id) { try { string sql = "SELECT * FROM Forum WHERE ID = @ID"; List <KeyValuePair <string, string> > parameters = new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>("ID", id.ToString()), }; DataSet results = ExecuteSql(sql, parameters); ForumDTO dto = DataSetParser.DataSetToForum(results, 0); return(dto); } catch (Exception e) { throw e; } }
public OperationDetails UpdateForum(ForumDTO forumDto) { try { if (forumDto == null) { return(new OperationDetails(false, "Empty forum has been provided", "")); } unitOfWork.ForumRepository.Update(MapperBag.ForumMapper.Map(forumDto)); unitOfWork.Save(); return(new OperationDetails(true, "Forum successfully updated", "")); } catch (Exception ex) { return(new OperationDetails(false, ex.Message, "")); } }
public HttpResponseMessage AddForum(ForumDTO dto) { try { Forums forum = new Forums { ForumId = dto.ForumId, Category = dto.Category, OwnerId = dto.OwnerId }; this._forumManagement.AddForum(forum); return(Request.CreateResponse(HttpStatusCode.OK)); } catch (Exception) { // Log exception code goes here return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error occured while executing method.")); } }
public long Insert(ForumDTO dto) { try { string sql = "INSERT INTO Forum(Title, Description, CreationDate, CreatorID) OUTPUT INSERTED.ID VALUES(@Title, @Description, CURRENT_TIMESTAMP, @CreatorID)"; List <KeyValuePair <string, string> > parameters = new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>("Title", dto.Title), new KeyValuePair <string, string>("Description", dto.Description), new KeyValuePair <string, string>("CreatorID", dto.CreatorID.ToString()), }; int results = ExecuteInsert(sql, parameters); return(results); } catch (Exception e) { throw e; } }
public List <ForumDTO> GetAll() { List <ForumDTO> forums = new List <ForumDTO>(); try { string sql = "SELECT * FROM Forum"; DataSet results = ExecuteSql(sql, new List <KeyValuePair <string, string> >()); for (int x = 0; x < results.Tables[0].Rows.Count; x++) { ForumDTO dto = DataSetParser.DataSetToForum(results, x); forums.Add(dto); } return(forums); } catch (Exception e) { throw e; } }
public ActionResult CreateForum(ForumsViewModel forumViewModel) { this.logger.Info("Entering: " + System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName + ": " + System.Reflection.MethodBase.GetCurrentMethod().Name + " --> " + User.Identity.Name); try { var _userId = Session["UserId"]; var _sessionUser = Convert.ToInt32(_userId); ForumDTO dto = new ForumDTO { ForumId = forumViewModel.ForumId, Category = forumViewModel.Category, OwnerId = _sessionUser }; //add new forum using (var client = new HttpClient()) { client.BaseAddress = new Uri(this.apiMethodsUrl); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json") ); //add to db HttpResponseMessage response = client.PostAsJsonAsync("api/discussions/AddForum/?forum=", dto).Result; if (!response.IsSuccessStatusCode) { throw new CustomException("Could not complete the operation!"); } } return(RedirectToAction("Forum")); } catch (CustomException ce) { this.logger.Trace(ce, "Username: "******"Operation could not be completed! Try again.\n" + ce.Message; return(View("Error")); } catch (Exception ex) { this.logger.Trace(ex, "Username: "******"Operation could not be completed!"; return(View("Error")); } }
public ActionResult <ForumDTO> CreateForum([FromHeader] string key, [FromBody] ForumDTO newForum) { if (!_authorizationService.AuthorizeUser(key)) { return(StatusCode(StatusCodes.Status401Unauthorized, "User authorization failed!")); } try { var created = _forumService.CreateForum(newForum); string location = linkGenerator.GetPathByAction("GetForumByID", "Forum", new { forumID = created.ForumID }); return(Created(location, created)); } catch (Exception ex) { logger.LogError(ex, "Error creating new forum: " + ex.Message); return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } }
public IActionResult UpdateForum([FromHeader] string key, [FromBody] ForumDTO updatedForum) { if (!_authorizationService.AuthorizeUser(key)) { return(StatusCode(StatusCodes.Status401Unauthorized, "User authorization failed!")); } var newForum = mapper.Map <Forum>(updatedForum); try { _forumService.UpdateForum(updatedForum); var res = mapper.Map <Forum>(newForum); return(Ok(res)); } catch (Exception ex) { logger.LogError(ex, "Error updating forum: " + ex.Message); return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } }
public ActionResult EditForum(ForumsViewModel forumViewModel) { this.logger.Info("Entering: " + System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName + ": " + System.Reflection.MethodBase.GetCurrentMethod().Name + " --> " + User.Identity.Name); try { //create the course dto to be passed to the api services ForumDTO dto = new ForumDTO { ForumId = forumViewModel.ForumId, Category = forumViewModel.Category, OwnerId = forumViewModel.OwnerId }; //edit the forum using (var client = new HttpClient()) { client.BaseAddress = new Uri(this.apiMethodsUrl); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json") ); HttpResponseMessage response = client.PutAsJsonAsync("api/discussions/UpdateForum/?id=" + forumViewModel.ForumId, dto).Result; if (!response.IsSuccessStatusCode) { throw new CustomException("Could not complete the operation!"); } } return(RedirectToAction("Forum")); } catch (CustomException ce) { this.logger.Trace(ce, "Username: "******"Operation could not be completed! Try again."; return(View("Error")); } catch (Exception ex) { this.logger.Trace(ex, "Username: "******"Operation could not be completed!"; return(View("Error")); } }
public async Task <OperationDetails> Delete(ForumDTO forumDto) { try { if (forumDto == null) { return(new OperationDetails(false, "Empty subforum has been provided", "")); } string forumName = forumDto.Name; if (forumName == null || forumName == string.Empty) { return(new OperationDetails(false, "Empty subforum name has been provided", "Name")); } unitOfWork.ForumRepository.Delete(forumDto.Id); await unitOfWork.SaveAsync(); return(new OperationDetails(true, "Forum was successfully deleted", forumName)); } catch (Exception ex) { return(new OperationDetails(false, ex.Message, "")); } }