public HttpResponseMessage PutArticle(int id, PocoArticles article) { HttpResponseMessage response; using (JournalEntities _entities = new JournalEntities()) { try { var myArticle = _entities.Articles.FirstOrDefault(c => c.Serial == id); /*var result = _entities.Articles.Select(c => new PocoArticles * { * authorId = c.AuthorID * }).ToList().FirstOrDefault(x => x.authorId == article.authorId);*/ if (myArticle == null) { response = Request.CreateErrorResponse(HttpStatusCode.NotFound, "the Author Id is not exist"); return(response); } else { var author = _entities.Authors.FirstOrDefault(c => c.Id == article.authorId); myArticle.Title = article.title; myArticle.Subject = article.subject; if (author != null) { myArticle.AuthorID = article.authorId; } else { myArticle.AuthorID = myArticle.AuthorID; } if (author != null && (article.authorId).GetType() == typeof(int) && (article.authorBirthYear).GetType() == typeof(int) && (article.authorWorkYears).GetType() == typeof(int) && !string.IsNullOrEmpty(article.authorFname) && !string.IsNullOrEmpty(article.authorLname)) { author.Id = article.authorId; author.Fname = article.authorFname; author.Lname = article.authorLname; author.BirthYear = article.authorBirthYear; author.WorkYears = article.authorWorkYears; myArticle.AuthorID = article.authorId; _entities.SaveChanges(); } else { response = Request.CreateErrorResponse(HttpStatusCode.Forbidden, "The Author id is not exist"); } _entities.SaveChanges(); response = Request.CreateResponse(HttpStatusCode.OK, " The Visitor User is Updated "); return(response); } } catch (Exception ex) { response = Request.CreateResponse(HttpStatusCode.BadRequest, ex); return(response); } } }
public HttpResponseMessage Post(PocoArticles article) { Article myArticle = new Article(); Author myAuthor = new Author(); HttpResponseMessage respone = new HttpResponseMessage(); using (JournalEntities _entities = new JournalEntities()) { try { if (article != null) { myArticle.AuthorID = article.authorId; myArticle.Title = article.title; myArticle.Subject = article.subject; myAuthor.Lname = article.authorLname; myAuthor.Fname = article.authorFname; myAuthor.BirthYear = article.authorBirthYear; myAuthor.WorkYears = article.authorWorkYears; var SID = _entities.Articles.FirstOrDefault(x => x.AuthorID == myArticle.AuthorID); if (myArticle.Title != "" && myArticle != null && SID != null) { _entities.Articles.Add(myArticle); _entities.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, myArticle); response = Request.CreateResponse(HttpStatusCode.OK, myArticle); } if (myAuthor.Fname != "" && myAuthor.Fname != null && myAuthor.Lname != null && myAuthor.Lname != "" && myAuthor != null) { _entities.Authors.Add(myAuthor); _entities.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, myAuthor); response = Request.CreateResponse(HttpStatusCode.OK, myAuthor); } else { respone = Request.CreateErrorResponse(HttpStatusCode.Forbidden, message: "The Article Values is NULL or Authors ID is wrong"); } return(respone); } else { return(respone = Request.CreateErrorResponse(HttpStatusCode.NoContent, "The article value is null")); } } catch (Exception ex) { return(respone = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } } }
public HttpResponseMessage GetaticleByname(string name) { HttpResponseMessage msg; try { JournalEntities _entities = new JournalEntities(); List <PocoArticles> result; result = _entities.Articles.Select(c => new PocoArticles { serial = c.Serial, title = c.Title, authorId = c.AuthorID, subject = c.Subject, authorFname = c.Author.Fname, authorLname = c.Author.Lname, authorBirthYear = c.Author.BirthYear, authorWorkYears = c.Author.WorkYears }).ToList(); PocoArticles Fresult = new PocoArticles(); Fresult = result.FirstOrDefault(c => c.title.ToLower() == name.ToLower()); if (Fresult != null) { msg = Request.CreateResponse(HttpStatusCode.Accepted, Fresult); return(msg); } else { msg = Request.CreateErrorResponse(HttpStatusCode.NotFound, "The Author name"); return(msg); } } catch (Exception ex) { msg = Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); return(msg); } }