New
- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
public class AuthorController : ApiController
{
GolflerDataModelEntities db = new GolflerDataModelEntities();
public IEnumerable<Author> GetAuthors()
{
return db.Authors.ToList<Author>();
}
[HttpGet]
public Response Details(long id)
{
if (id <= 0)
{
return new Models.Response { Status = 0, Message = "Valid id is required", Record = null };
}
var author = db.Authors.FirstOrDefault(x => x.Id == id);
if (author == null)
{
return new Models.Response { Message = "Recod Not Found", Status = 0, Record = null };
}
//var response = Vendor.Details();
return new Models.Response
{
Status = 1,
Message = "Success",
Record = new
{
Author = author
}
};
}
[HttpPost]
public Response Add([FromBody] Author AuthorRequestBody)
{
Author author = new Author();
author = db.Authors.Add(
db.SaveChanges();
var response = author;
return new Models.Response
{
Status = 1,
Message = "author Added Successfully",
Record = new
{
Vendor = response
}
};
}
[HttpPut]
public Response Edit([FromBody]Author AuthorRequestBody)
{
var author = db.Authors.FirstOrDefault(x => x.Id == AuthorRequestBody.Id);
if (author == null)
{
return new Models.Response { Status = 0, Message = "Author Not Found", };
}
if (AuthorRequestBody.First_Name != null)
{
author.First_Name = AuthorRequestBody.First_Name;
}
if (AuthorRequestBody.Last_Name != null)
{
author.Last_Name = AuthorRequestBody.Last_Name;
}
db.SaveChanges();
var response = author;
return new Response
{
Status = 1,
Message = "Author Updated Successfully",
Record = new
{
author = response
}
};
}
[HttpDelete]
public dynamic Delete(long ID)
{
Author author = db.Authors.Find(ID);
if (author == null)
{
return NotFound();
}
db.Authors.Remove(author);
db.SaveChanges();
return Ok(author);
}
}
|
Comments
Post a Comment