first create table
create table book
( ID int identity(1,1),
BooKName varchar(50)
)
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace RestBookexample
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
List<Booki> GetBooksList();
[OperationContract]
List<Booki> GetBookById(string id);
[OperationContract]
void AddBook(string name);
[OperationContract]
void UpdateBook(string id, string name);
[OperationContract]
void DeleteBook(string id);
// TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class Booki
{
int Id = 0;
string bookName ="";
[DataMember]
public string BookName
{
get { return bookName; }
set { bookName = value; }
}
[DataMember]
public int ID
{
get { return Id; }
set { Id = value; }
}
}
}
create table book
( ID int identity(1,1),
BooKName varchar(50)
)
-------------------------------------------------------------------------------------------
using System;using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace RestBookexample
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
List<Booki> GetBooksList();
[OperationContract]
List<Booki> GetBookById(string id);
[OperationContract]
void AddBook(string name);
[OperationContract]
void UpdateBook(string id, string name);
[OperationContract]
void DeleteBook(string id);
// TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class Booki
{
int Id = 0;
string bookName ="";
[DataMember]
public string BookName
{
get { return bookName; }
set { bookName = value; }
}
[DataMember]
public int ID
{
get { return Id; }
set { Id = value; }
}
}
}
----------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace RestBookexample
{
public class Service1 : IService1
{
public List<Booki> GetBooksList()
{
test_dbEntities entities = new test_dbEntities();
Booki boo = null;
List<Booki> booklist = new List<Booki>();
for (int i = 0; i < entities.books.ToList().Count; i++)
{
boo = new Booki();
boo.BookName = entities.books.ToList()[i].BooKName;
boo.ID = entities.books.ToList()[i].ID;
booklist.Add(boo);
}
return booklist;
}
public List<Booki> GetBookById(string id)
{
int bookId = Convert.ToInt32(id);
List<Booki> booklist = new List<Booki>();
try
{
test_dbEntities entities = new test_dbEntities();
Booki boo = null;
for (int i = 0; i < entities.books.ToList().Count; i++)
{
boo = new Booki();
boo.BookName = entities.books.ToList()[i].BooKName;
boo.ID = entities.books.ToList()[i].ID;
booklist.Add(boo);
}
}
catch
{
throw new FaultException("Something went wrong");
}
return booklist.Where(em => em.ID == bookId).ToList().Take(1).ToList();
}
public void AddBook(string name)
{
test_dbEntities entities = new test_dbEntities();
book boo = new book();
boo.BooKName = name;
entities.AddTobooks(boo);
entities.SaveChanges();
}
public void UpdateBook(string id, string name)
{
try
{
int bookId = Convert.ToInt32(id);
using (test_dbEntities entities = new test_dbEntities())
{
book book1 = entities.books.SingleOrDefault(b => b.ID == bookId);
book1.BooKName = name;
entities.SaveChanges();
}
}
catch
{
throw new FaultException("Something went wrong");
}
}
public void DeleteBook(string id)
{
try
{
int bookId = Convert.ToInt32(id);
using (test_dbEntities entities = new test_dbEntities())
{
book book1 = entities.books.SingleOrDefault(b => b.ID == bookId);
entities.DeleteObject(book1);
entities.SaveChanges();
}
}
catch
{
throw new FaultException("Something went wrong");
}
}
}
}
No comments:
Post a Comment