public IHttpActionResult Update(Poco.Issue item) { if (item.IssueId <= 0) { return(BadRequest("Issue ID is not valid!")); } if (String.IsNullOrWhiteSpace(item.Title)) { return(BadRequest("Title is not valid!")); } if (String.IsNullOrWhiteSpace(item.Description)) { return(BadRequest("Description is not valid!")); } try { using (var ctx = new BugghyDbContext()) { Issue ix = ctx.Issues.SingleOrDefault(x => x.IssueId == item.IssueId); if (ix == null) { return(InternalServerError(new InvalidOperationException("Invalid Issue ID!"))); } ix.Title = item.Title; ix.Description = item.Description; ctx.SaveChanges(); return(Ok(Dto.Wrap(new Poco.Issue { IssueId = ix.IssueId, GimmickId = ix.GimmickId, UserId = ix.UserId, Sender = ix.Sender, Code = ix.Code, Title = ix.Title, Description = ix.Description, Type = ix.Type, Status = ix.Status, CreationDate = ix.CreationDate, ReplyDate = ix.ReplyDate, ClosedDate = ix.ClosedDate, IsClosed = ix.IsClosed }))); } } catch (Exception ex) { return(InternalServerError(ex)); } }
public IHttpActionResult AddNew(Poco.Issue item) { if (item.GimmickId <= 0) { return(BadRequest("Gimmick ID is not valid!")); } if (item.UserId <= 0) { return(BadRequest("User ID is not valid!")); } if (String.IsNullOrWhiteSpace(item.Title)) { return(BadRequest("Title is not valid!")); } if (String.IsNullOrWhiteSpace(item.Description)) { return(BadRequest("Description is not valid!")); } try { using (var ctx = new BugghyDbContext()) { User user = ctx.Users.SingleOrDefault(x => x.UserId == item.UserId); if (user == null) { throw new InvalidOperationException("Unable to find an user with the ID specified!"); } Issue ix = new Issue { GimmickId = item.GimmickId, UserId = item.UserId, Sender = user.Email, Title = item.Title, Description = item.Description, Type = item.Type, Status = IssueStatus.Opened, CreationDate = DateTime.Now.ToUniversalTime() }; ctx.Issues.Add(ix); ctx.SaveChanges(); // Updating code ix.Code = String.Concat( ix.Type.ToString().Substring(0, 1), "-", ix.IssueId.ToString()); ctx.SaveChanges(); return(Ok(Dto.Wrap(new Poco.Issue { IssueId = ix.IssueId, GimmickId = ix.GimmickId, UserId = ix.UserId, Sender = ix.Sender, Code = ix.Code, Title = ix.Title, Description = ix.Description, Type = ix.Type, Status = ix.Status, CreationDate = ix.CreationDate, ReplyDate = ix.ReplyDate, ClosedDate = ix.ClosedDate, IsClosed = ix.IsClosed }))); } } catch (Exception ex) { return(InternalServerError(ex)); } }