public void AddTweetReaction(ReactionDTO reactionDTO) { try { int userId = db.Users.Where(x => x.Email == reactionDTO.Email) .Select(x => x.UserId).FirstOrDefault(); reactionDTO.UserId = userId; var reaction = db.Reactions .Where(x => x.UserId == userId && x.TweetId == reactionDTO.TweetId).FirstOrDefault(); if (reaction == null) { Reaction reactionObj = new Reaction(); reactionObj.UserId = userId; reactionObj.TweetId = reactionDTO.TweetId; reactionObj.LikeFlag = reactionDTO.LikeFlag; db.Reactions.Add(reactionObj); Save(); } else { // if user already liked or unliked then change likeflag status reaction.LikeFlag = reactionDTO.LikeFlag; Save(); } } catch (Exception) { Console.WriteLine("Error in database connection."); } }
public static async Task FinishSketchReactionAsync(int id, ReactionDTO reaction) { await Task.Run(() => { using (var db = new ChContext()) { var finishreaction = db.Reactions.Where(r => r.ID == id).First(); finishreaction.ClosureDate = reaction.ClosureDate; finishreaction.ProcedureText = reaction.Procedure; finishreaction.Yield = reaction.Yield; finishreaction.Observation = reaction.Observation; foreach (var item in reaction.ObservationImgs) { var tmp = new ObservationImg { img = item, Reaction = finishreaction }; db.ObservationImgs.Add(tmp); finishreaction.ObservationImgs.Add(tmp); } finishreaction.Sketch = false; db.SaveChanges(); } }); }
public static async Task <ReactionDTO> GetReactionAsync(int reactionId) { ReactionDTO result = new ReactionDTO(); await Task.Run(() => { using (var db = new ChContext()) { var query = db.Reactions.Where(r => r.ID == reactionId).First(); result = new ReactionDTO() { Code = query.ReactionCode, Chemist = query.Chemist.Name, Chiefchemist = query.Chiefchemist.Name, Project = query.Project.Name, Laboratory = query.Laboratory, StartDate = query.StartDate, Observation = query.Observation, Procedure = query.ProcedureText, Yield = query.Yield, IsSketch = query.Sketch.Value, ReactionImg = query.ReactionImg }; if (query.ClosureDate.HasValue) { result.ClosureDate = query.ClosureDate.Value; } if (query.PreviousStep != null) { result.PreviousStep = query.PreviousStep.ReactionCode; } } }); return(result); }
public IActionResult CreateReaction([FromBody] ReactionDTO reaction, [FromHeader] int UserId, [FromHeader] int PostId) { if (_user.GetUserById(UserId) == null || _post.GetPostById(PostId) == null) { return(StatusCode(StatusCodes.Status400BadRequest)); } var checkReaction = _context.Reactions.FirstOrDefault(u => u.PostId == PostId && u.CreatedByUserId == UserId); if (checkReaction != null) { return(StatusCode(StatusCodes.Status400BadRequest)); } var newReaction = new Reaction() { CreatedByUserId = UserId, PostId = PostId, ReactionTypeID = reaction.ReactionTypeId }; if (_context.ReactionTypes.Find(reaction.ReactionTypeId) == null) { return(StatusCode(StatusCodes.Status400BadRequest)); } _context.Reactions.Add(newReaction); var success = _context.SaveChanges(); if (success < 1) { return(StatusCode(StatusCodes.Status400BadRequest)); } return(StatusCode(StatusCodes.Status201Created, new JsonResult(newReaction))); }
public IActionResult UpdateReaction(int id, [FromBody] ReactionDTO reaction, [FromHeader] int UserID, [FromHeader] int PostID) { var currReaction = _context.Reactions.Find(id); if (currReaction == null) { return(StatusCode(StatusCodes.Status404NotFound)); } if (_user.GetUserById(UserID) == null || _post.GetPostById(PostID) == null) { return(StatusCode(StatusCodes.Status400BadRequest)); } if (UserID != currReaction.CreatedByUserId || PostID != currReaction.PostId) { return(StatusCode(StatusCodes.Status403Forbidden)); } currReaction.ReactionTypeID = reaction.ReactionTypeId; _context.Reactions.Update(currReaction); var success = _context.SaveChanges(); if (success < 1) { return(StatusCode(StatusCodes.Status400BadRequest)); } return(StatusCode(StatusCodes.Status202Accepted)); }
public IHttpActionResult RemoveReaction(ReactionDTO reactionDTO) { if (!ModelState.IsValid) { return(BadRequest("Model state not valid. Check parameters")); } Post post = _context.Posts.SingleOrDefault(p => p.TimeStamp.Equals(reactionDTO.MessageTimeStamp)); if (post != null && reactionDTO.SlackUserId != post.SlackUserId) { if (reactionDTO.Reaction.Equals("+1")) { post.AddUpVotesToPost(-1); } if (reactionDTO.Reaction.Equals("-1")) { post.AddDownVotesToPost(-1); } undoAddPointsToUsers(post, reactionDTO); _context.SaveChanges(); } return(Ok()); }
private void addPointsToUsers(Post post, ReactionDTO reactionDTO) { Points points; if (reactionDTO.Reaction.Equals("+1")) { points = RefereeHelper.Instance.VoterVotedAnswerUp(); } else if (reactionDTO.Reaction.Equals("-1")) { points = RefereeHelper.Instance.VoterVotedAnswerDown(); } else { return; } var voter = _context.Users.SingleOrDefault(v => v.SlackId.Equals(reactionDTO.SlackUserId)); if (voter != null) { voter.AddUserPoints(points.VoterPoints); } //Adding points to the Original Poster if (post != null && post.ApplicationUser != null) { post.ApplicationUser.AddUserPoints(points.PosterPoints); } }
public async Task LikePost(NewReactionDTO reaction) { var reactionsDTO = new ReactionDTO(); reactionsDTO.IsLike = reaction.IsLike; var user = _context.Users.Where(user => user.Id == reaction.UserId).FirstOrDefault(); reactionsDTO.User = _mapper.Map <UserDTO>(user); var likes = _context.PostReactions.Where(x => x.UserId == reaction.UserId && x.PostId == reaction.EntityId); if (likes.Any() && likes.Select(l => l.IsLike).FirstOrDefault() == reaction.IsLike) { _context.PostReactions.RemoveRange(likes); await _context.SaveChangesAsync(); var firstPost = GetPost(reaction).Result; var firstPostDTO = _mapper.Map <PostDTO>(firstPost); await _postHub.Clients.All.SendAsync("Like", firstPostDTO); return; } if (!likes.Any()) { _context.PostReactions.Add(new DAL.Entities.PostReaction { PostId = reaction.EntityId, IsLike = reaction.IsLike, UserId = reaction.UserId }); await _context.SaveChangesAsync(); var secondPost = GetPost(reaction).Result; var secondPostDTO = _mapper.Map <PostDTO>(secondPost); await _postHub.Clients.All.SendAsync("Like", secondPostDTO); return; } _context.PostReactions.RemoveRange(likes); await _context.SaveChangesAsync(); _context.PostReactions.Add(new DAL.Entities.PostReaction { PostId = reaction.EntityId, IsLike = reaction.IsLike, UserId = reaction.UserId }); await _context.SaveChangesAsync(); var post = GetPost(reaction).Result; var postDTO = _mapper.Map <PostDTO>(post); await _postHub.Clients.All.SendAsync("Like", postDTO); }
public IHttpActionResult PostTweetReaction(ReactionDTO reactionDTO) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } reactionBs.AddTweetReaction(reactionDTO); return(Ok(reactionDTO)); }
public ActionResult <Post> AddReaction(int postId, [FromBody] ReactionDTO reaction) { Post post = _postRepository.GetBy(postId); Reaction createdReaction = new Reaction() { ReactionDescription = reaction.ReactionDescription, ReactionId = 0, CreatorName = reaction.CreatorName, DateAdded = post.DateAdded }; post.AddReaction(createdReaction); _postRepository.SaveChanges(); return(CreatedAtAction(nameof(AddReaction), new { id = createdReaction.ReactionId }, createdReaction)); }
protected override void Because() { _result = sut.MapFrom(_inputReaction); }
public async Task AddReaction(ReactionInfo reaction) { ReactionDTO reactionDTO = new ReactionDTO { Code = reaction.Code, Chemist = reaction.Chemist, Chiefchemist = reaction.Chiefchemist, Project = reaction.Project, Laboratory = reaction.Laboratory, StartDate = reaction.StartDate, ClosureDate = reaction.ClosureDate, PreviousStep = reaction.PreviousStep, Literature = reaction.Literature, IsSketch = reaction.IsSketch, ReactionImg = convertImg(reaction.ReactionImgPath), Procedure = reaction.Procedure, Yield = reaction.Yield, Observation = reaction.Observation }; StartingMaterialDTO smDTO = new StartingMaterialDTO { MoleculeCAS = reaction.StartingMaterial.CAS, ReactionName = reaction.Code, mValue = reaction.StartingMaterial.mValue, nValue = reaction.StartingMaterial.nValue, VValue = reaction.StartingMaterial.VValue, Location = reaction.StartingMaterial.Location }; List <ReagentDTO> reagentDTOs = new List <ReagentDTO>(); foreach (var item in reaction.Reagents) { reagentDTOs.Add(new ReagentDTO { MoleculeCAS = item.CAS, ReactionName = reaction.Code, Ratio = item.Ratio, Location = item.Location, mValue = item.mValue, VValue = item.VValue }); } List <SolventDTO> solventDTOs = new List <SolventDTO>(); foreach (var item in reaction.Solvents) { solventDTOs.Add(new SolventDTO { MoleculeCAS = item.CAS, ReactionName = reaction.Code, VValue = item.VValue, Location = item.Location }); } List <ProductDTO> productDTOs = new List <ProductDTO>(); foreach (var item in reaction.Products) { productDTOs.Add(new ProductDTO { ReactionName = reaction.Code, MW = item.mValue, Ratio = item.Ratio }); } List <byte[]> observationImgs = new List <byte[]>(); foreach (var item in reaction.ObservationImgPaths) { observationImgs.Add(convertImg(item)); } reactionDTO.StartingMaterial = smDTO; reactionDTO.Reagents = reagentDTOs; reactionDTO.Solvents = solventDTOs; reactionDTO.Products = productDTOs; reactionDTO.ObservationImgs = observationImgs; await DbAccess.AddReactionAsync(reactionDTO); }
public static async Task AddReactionAsync(ReactionDTO reactionDTO) { await Task.Run(() => { using (var db = new ChContext()) { var reaction = reactionDTO.TransformToReaction(); reaction.Chemist = db.People.Where(p => p.Name == reactionDTO.Chemist).First(); reaction.Chiefchemist = db.People.Where(p => p.Name == reactionDTO.Chiefchemist).First(); reaction.Project = db.Projects.Where(p => p.Name == reactionDTO.Project).First(); if (!string.IsNullOrEmpty(reactionDTO.PreviousStep)) { reaction.PreviousStep = db.Reactions.Where(re => re.ReactionCode == reactionDTO.PreviousStep).First(); } if (!reaction.Sketch.Value) { foreach (var item in reactionDTO.ObservationImgs) { var tmp = new ObservationImg { img = item, Reaction = reaction }; db.ObservationImgs.Add(tmp); reaction.ObservationImgs.Add(tmp); } } var tmpsm = reactionDTO.StartingMaterial.TransformToStartingMaterial(); tmpsm.Reaction = reaction; db.StartingMaterials.Add(tmpsm); reaction.StartingMaterials.Add(tmpsm); foreach (var item in reactionDTO.Reagents) { var tmp = item.TransformToReagent(); tmp.Reaction = reaction; db.Reagents.Add(tmp); reaction.Reagents.Add(tmp); } foreach (var item in reactionDTO.Solvents) { var tmp = item.TransformToSolvent(); tmp.Reaction = reaction; db.Solvents.Add(tmp); reaction.Solvents.Add(tmp); } foreach (var item in reactionDTO.Products) { var tmp = item.TransformToProduct(); tmp.Reaction = reaction; db.Products.Add(tmp); reaction.Products.Add(tmp); } db.Reactions.Add(reaction); db.SaveChanges(); //var sm1 = db.LocationMolecules.Where(m => m.MoleculeCAS == reactionDTO.StartingMaterial.MoleculeCAS && m.Location.Code == reactionDTO.StartingMaterial.Location).ToList(); var sm = db.LocationMolecules.Where(m => m.MoleculeCAS == reactionDTO.StartingMaterial.MoleculeCAS && m.Location.Code == reactionDTO.StartingMaterial.Location).First(); if (sm.v.HasValue) { sm.v = sm.v - reactionDTO.StartingMaterial.VValue; } else { sm.m = sm.m - reactionDTO.StartingMaterial.mValue; } db.SaveChanges(); LocationMolecule r = new LocationMolecule(); //LocationMolecule r2 = new LocationMolecule(); foreach (var item in reactionDTO.Reagents) { //var r2 = db.LocationMolecules.Where(m => m.MoleculeCAS == item.MoleculeCAS && m.Location.Code == item.Location).ToList(); r = db.LocationMolecules.Where(m => m.MoleculeCAS == item.MoleculeCAS && m.Location.Code == item.Location).First(); if (r.v.HasValue) { r.v = r.v - item.VValue; } else { r.m = r.m - item.mValue; } db.SaveChanges(); } LocationMolecule s = new LocationMolecule(); foreach (var item in reactionDTO.Solvents) { //var s2 = db.LocationMolecules.Where(m => m.MoleculeCAS == item.MoleculeCAS && m.Location.Code == item.Location).ToList(); s = db.LocationMolecules.Where(m => m.MoleculeCAS == item.MoleculeCAS && m.Location.Code == item.Location).First(); s.v = s.v - item.VValue; db.SaveChanges(); } } }); }
public void BindTo(ReactionDTO dtoReaction) { lblStoichiometric.Text = dtoReaction.Stoichiometric; txtName.Text = dtoReaction.Name; htmlEditor.Text = dtoReaction.Description; }
public void AddTweetReaction(ReactionDTO reactionDTO) { objDb.AddTweetReaction(reactionDTO); }