public IHttpActionResult Update(int id, CodeSnipetDataModel codeSnipet) { if (!this.ModelState.IsValid) { return BadRequest(ModelState); } var existingCodeSnipet = this.data.CodeSnipets.All().FirstOrDefault(a => a.Id == id); if (existingCodeSnipet == null) { return BadRequest(NO_CODE_SNIPET); } var currentUserId = this.userIdProvider.GetUserId(); if (existingCodeSnipet.UserId != currentUserId) { return BadRequest(NOT_YOUR_SNIPET); } existingCodeSnipet.Content = codeSnipet.Content; existingCodeSnipet.Language = codeSnipet.Language; existingCodeSnipet.Title = codeSnipet.Title; this.data.SaveChanges(); codeSnipet.Id = id; codeSnipet.UserId = existingCodeSnipet.UserId; codeSnipet.AddedOn = existingCodeSnipet.AddedOn; codeSnipet.Score = CalculateScoreForSnipet(id); UpdateLastActivityForUser(); return Ok(codeSnipet); }
public IHttpActionResult Create(CodeSnipetDataModel codeSnipet) { if (!this.ModelState.IsValid) { return BadRequest(ModelState); } var currentUserId = this.userIdProvider.GetUserId(); var newCodeSnipet = new CodeSnipet { Content = codeSnipet.Content, Language = codeSnipet.Language, Title = codeSnipet.Title, UserId = currentUserId, AddedOn = DateTime.Now }; this.data.CodeSnipets.Add(newCodeSnipet); this.data.SaveChanges(); UpdateLastActivityForUser(); return Ok(newCodeSnipet.Id); }