public static WebApiTicket Login(string login, string password, int minutes, AriUMContext ctx) { WebApiTicket tk = null; // First verify if a user with this credentials exists User user = (from u in ctx.Users where u.Login == login select u).FirstOrDefault<User>(); if (user != null) { // User exists. Does the password match? if (user.Password == GetHashCode(password)) { // Go to get the ticket string code = GenerateTicket(); tk = new WebApiTicket() { Code = code, Start = DateTime.Now, User = user }; tk.End = tk.Start.AddMinutes(minutes); } } return tk; }
public static void CreateDefaultRegisters() { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { // Some registers to start UserGroup usp = new UserGroup() { Name="Technical staff" }; CntWebApiVerbs.PostUserGroup(usp, ctx); User u = new User() { Name = "John Doe", Email = "*****@*****.**" }; u.UserGroup = usp; CntWebApiVerbs.PostUser(u, ctx); u = new User() { Name="Martha Graham", Email="*****@*****.**" }; u.UserGroup = usp; CntWebApiVerbs.PostUser(u, ctx); } }
// POST api/users public virtual HttpResponseMessage Post(User user) { if (user == null) { return Request.CreateResponse(HttpStatusCode.BadRequest); } else { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { if (user.UserGroup != null) { int id2 = user.UserGroup.UserGroupId; user.UserGroup = CntWebApiVerbs.GetUserGroup(id2, ctx); } User u = CntWebApiVerbs.PostUser(user, ctx); FetchStrategy fs = new FetchStrategy(); fs.LoadWith<User>(x => x.UserGroup); User ud = ctx.CreateDetachedCopy<User>(u, fs); var response = Request.CreateResponse<User>(HttpStatusCode.Created, ud); response.Headers.Location = GetUserGroupLocation(ud.UserId); return response; } } }
// GET api/products public IEnumerable<Product> Get() { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { IEnumerable<Product> product = CntWebApiVerbs.GetProducts(ctx); IEnumerable<Product> cS = ctx.CreateDetachedCopy<IEnumerable<Product>>(product); return cS; } }
// GET api/customers public IEnumerable<Customer> Get() { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { IEnumerable<Customer> customers = CntWebApiVerbs.GetCustomers(ctx); IEnumerable<Customer> cS = ctx.CreateDetachedCopy<IEnumerable<Customer>>(customers); return cS; } }
// GET api/usergroups /// <summary> /// Get all user groups from the server /// </summary> /// <returns>A list of all user groups</returns> public HttpResponseMessage Get() { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { IEnumerable<UserGroup> userGroups = CntWebApiVerbs.GetUserGroups(ctx); FetchStrategy fs = new FetchStrategy(); IEnumerable<UserGroup> uGs = ctx.CreateDetachedCopy<IEnumerable<UserGroup>>(userGroups, fs); return Request.CreateResponse<IEnumerable<UserGroup>>(HttpStatusCode.OK, uGs); } }
// GET api/invoiceLines public IEnumerable<InvoiceLine> Get() { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { IEnumerable<InvoiceLine> invoiceLine = CntWebApiVerbs.GetInvoiceLines(ctx); FetchStrategy fs = new FetchStrategy(); fs.LoadWith<InvoiceLine>(x => x.Product, x => x.Invoice); IEnumerable<InvoiceLine> iS = ctx.CreateDetachedCopy<IEnumerable<InvoiceLine>>(invoiceLine, fs); return iS; } }
// GET api/users public IEnumerable<User> Get() { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { IEnumerable<User> user = CntWebApiVerbs.GetUsers(ctx); FetchStrategy fs = new FetchStrategy(); fs.LoadWith<User>(x => x.UserGroup); IEnumerable<User> uS = ctx.CreateDetachedCopy<IEnumerable<User>>(user, fs); return uS; } }
public IEnumerable<Customer> Get(string order) { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { IEnumerable<Customer> customers = (from c in ctx.Customers orderby c.Name select c).ToList<Customer>(); IEnumerable<Customer> cS = ctx.CreateDetachedCopy<IEnumerable<Customer>>(customers); return cS; } }
// GET api/products public IEnumerable<Invoice> Get() { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { IEnumerable<Invoice> invoices = CntWebApiVerbs.GetInvoices(ctx); FetchStrategy fs = new FetchStrategy(); fs.LoadWith<Invoice>(x => x.Customer); IEnumerable<Invoice> cS = ctx.CreateDetachedCopy<IEnumerable<Invoice>>(invoices, fs); return cS; } }
// DELETE api/invoiceLines/5 public virtual HttpResponseMessage Delete(int id) { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { InvoiceLine il = CntWebApiVerbs.GetInvoiceLine(id, ctx); if (il != null) { CntWebApiVerbs.DeleteInvoiceLine(il, ctx); } return Request.CreateResponse(HttpStatusCode.OK); } }
// DELETE api/products/5 public virtual HttpResponseMessage Delete(int id) { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { Product u = CntWebApiVerbs.GetProduct(id, ctx); if (u != null) { CntWebApiVerbs.DeleteProduct(u, ctx); } return Request.CreateResponse(HttpStatusCode.OK); } }
/// <summary> /// Deletes the user group with a given id /// </summary> /// <param name="id">Id of the user group to be deleted</param> /// <returns></returns> public virtual HttpResponseMessage Delete(int id) { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { UserGroup uG = CntWebApiVerbs.GetUserGroup(id, ctx); if (uG != null) { CntWebApiVerbs.DeleteUserGroup(uG, ctx); } return Request.CreateResponse(HttpStatusCode.OK); } }
public IEnumerable<InvoiceLine> GetLinesFromInvoice(int InvoiceId) { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { IEnumerable<InvoiceLine> invoiceLine = (from il in ctx.InvoiceLines where il.Invoice.InvoiceId == InvoiceId select il).ToList<InvoiceLine>(); FetchStrategy fs = new FetchStrategy(); fs.LoadWith<InvoiceLine>(x => x.Product, x => x.Invoice); IEnumerable<InvoiceLine> iS = ctx.CreateDetachedCopy<IEnumerable<InvoiceLine>>(invoiceLine, fs); return iS; } }
public static bool CheckTicket(string code, AriUMContext ctx) { // Current date time DateTime curtime = DateTime.Now; // look for a ticket with this code and active WebApiTicket tk = (from t in ctx.WebApiTickets where t.Code == code && t.End > curtime select t).FirstOrDefault<WebApiTicket>(); if (tk != null) return true; else return false; }
// GET api/products/5 public virtual Product Get(int id) { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { Product product = CntWebApiVerbs.GetProduct(id, ctx); if (product == null) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); } else { Product u = ctx.CreateDetachedCopy<Product>(product); return u; } } }
// GET api/customers/5 public virtual Customer Get(int id) { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { Customer customer = CntWebApiVerbs.GetCustomer(id, ctx); if (customer == null) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); } else { Customer u = ctx.CreateDetachedCopy<Customer>(customer); return u; } } }
/// <summary> /// Get an individual user group /// </summary> /// <param name="id">User groups' id you want</param> /// <returns>Use group object (XML/JSON)</returns> public virtual UserGroup Get(int id) { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { UserGroup userGroup = CntWebApiVerbs.GetUserGroup(id, ctx); if (userGroup == null) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); } else { FetchStrategy fs = new FetchStrategy(); UserGroup uG = ctx.CreateDetachedCopy<UserGroup>(userGroup, fs); return uG; } } }
// POST api/customers public virtual HttpResponseMessage Post(Customer customer) { if (customer == null) { return Request.CreateResponse(HttpStatusCode.BadRequest); } else { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { Customer c = CntWebApiVerbs.PostCustomer(customer, ctx); Customer cd = ctx.CreateDetachedCopy<Customer>(c); var response = Request.CreateResponse<Customer>(HttpStatusCode.Created, cd); response.Headers.Location = GetCustomerGroupLocation(cd.CustomerId); return response; } } }
// GET api/invoiceLines/5 public virtual InvoiceLine Get(int id) { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { InvoiceLine invoiceLine = CntWebApiVerbs.GetInvoiceLine(id, ctx); if (invoiceLine == null) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); } else { FetchStrategy fs = new FetchStrategy(); fs.LoadWith<InvoiceLine>(x => x.Product, x => x.Invoice); InvoiceLine il = ctx.CreateDetachedCopy<InvoiceLine>(invoiceLine, fs); return il; } } }
// POST api/products public virtual HttpResponseMessage Post(Product product) { if (product == null) { return Request.CreateResponse(HttpStatusCode.BadRequest); } else { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { Product c = CntWebApiVerbs.PostProduct(product, ctx); Product cd = ctx.CreateDetachedCopy<Product>(c); var response = Request.CreateResponse<Product>(HttpStatusCode.Created, cd); response.Headers.Location = GetProductGroupLocation(cd.ProductId); return response; } } }
/// <summary> /// Gets the user group with a given name /// </summary> /// <param name="name">The name of the object</param> /// <returns>User group object</returns> public virtual UserGroup GetByName(string name) { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { UserGroup userGroup = (from ug in ctx.UserGroups where ug.Name == name select ug).FirstOrDefault<UserGroup>(); if (userGroup == null) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); } else { FetchStrategy fs = new FetchStrategy(); UserGroup uG = ctx.CreateDetachedCopy<UserGroup>(userGroup, fs); return uG; } } }
public virtual HttpResponseMessage GetLogin(string login, string password) { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { WebApiTicket tck = CntWebApiSecurity.Login(login,password,30,ctx); if (tck == null) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Nombre de usuario o contraseña incorrecto"); } else { // we must add the new ticket to the database ctx.Add(tck); ctx.SaveChanges(); tck = ctx.CreateDetachedCopy<WebApiTicket>(tck, x => x.User); return Request.CreateResponse<WebApiTicket>(HttpStatusCode.OK, tck); } } }
// POST api/products public virtual HttpResponseMessage Post(Invoice invoice) { if (invoice == null) { return Request.CreateResponse(HttpStatusCode.BadRequest); } else { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { if (invoice.Customer != null) { // Customer customer = (from c in ctx.Customers where c.CustomerId == invoice.Customer.CustomerId select c).FirstOrDefault<Customer>(); if (customer != null) { invoice.Customer = customer; } else { } } // Calculate invoice number int maxInvoiceNumber = (from inv in ctx.Invoices where inv.Year == invoice.Year select inv.InvoiceNumber).Max(); invoice.InvoiceNumber = maxInvoiceNumber + 1; Invoice i = CntWebApiVerbs.PostInvoice(invoice, ctx); FetchStrategy fs = new FetchStrategy(); fs.LoadWith<Invoice>(x => x.Customer); Invoice idt = ctx.CreateDetachedCopy<Invoice>(i,fs); var response = Request.CreateResponse<Invoice>(HttpStatusCode.Created, idt); response.Headers.Location = GetInvoiceLocation(idt.InvoiceId); return response; } } }
/// <summary> /// Updates the given user group /// </summary> /// <param name="id">The id of the user group to be updated</param> /// <param name="userGroup">User group with the modifications you want</param> /// <returns></returns> public virtual HttpResponseMessage Put(int id, UserGroup userGroup, string tk) { if (userGroup == null || id != userGroup.UserGroupId) { return Request.CreateResponse(HttpStatusCode.BadRequest); } else { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { // Does it exist? UserGroup ug = CntWebApiVerbs.GetUserGroup(id, ctx); if (ug == null) { return Request.CreateResponse(HttpStatusCode.NotFound); } else { CntWebApiVerbs.PutUserGroup(userGroup, ctx); return Request.CreateResponse(HttpStatusCode.NoContent); } } } }
/// <summary> /// Creates a new user group /// </summary> /// <param name="userGroup">The user group that yo want to create</param> /// <returns>Url related to the new object</returns> public virtual HttpResponseMessage Post(UserGroup userGroup) { if (userGroup == null) { return Request.CreateResponse(HttpStatusCode.BadRequest); } else { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { UserGroup uG = CntWebApiVerbs.PostUserGroup(userGroup, ctx); FetchStrategy fs = new FetchStrategy(); UserGroup uGd = ctx.CreateDetachedCopy<UserGroup>(uG, fs); var response = Request.CreateResponse<UserGroup>(HttpStatusCode.Created, uGd); response.Headers.Location = GetUserGroupLocation(uGd.UserGroupId); return response; } } }
/// <summary> /// Get all user groups ordered alphabetically /// </summary> /// <param name="order">Indicates what order you want , so far it orders by name only</param> /// <returns></returns> public IEnumerable<UserGroup> GetOrdered(string order) { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { IEnumerable<UserGroup> userGroups = (from ug in ctx.UserGroups orderby ug.Name select ug).ToList<UserGroup>(); FetchStrategy fs = new FetchStrategy(); IEnumerable<UserGroup> uGs = ctx.CreateDetachedCopy<IEnumerable<UserGroup>>(userGroups, fs); return uGs; } }
// POST api/invoiceLines public virtual HttpResponseMessage Post(InvoiceLine invoiceLine) { if (invoiceLine == null) { return Request.CreateResponse(HttpStatusCode.BadRequest); } else { if (invoiceLine.Product == null) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No se puede crear una línea sin producto"); } if (invoiceLine.Invoice == null) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No se puede crear una línea de factura sin información de su cabecera"); } using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { InvoiceLine i = CntWebApiVerbs.PostInvoiceLine(invoiceLine, ctx); InvoiceLine idt = ctx.CreateDetachedCopy<InvoiceLine>(i); var response = Request.CreateResponse<InvoiceLine>(HttpStatusCode.Created, idt); response.Headers.Location = GetInvoiceLineLocation(idt.InvoiceLineId); return response; } } }
// PUT api/invoiceLines/5 public virtual HttpResponseMessage Put(int id, InvoiceLine invoiceLine) { if (invoiceLine == null || id != invoiceLine.InvoiceLineId) { return Request.CreateResponse(HttpStatusCode.BadRequest); } else { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { // Does it exist? InvoiceLine i = CntWebApiVerbs.GetInvoiceLine(id, ctx); if (i == null) { return Request.CreateResponse(HttpStatusCode.NotFound); } else { CntWebApiVerbs.PutInvoiceLine(invoiceLine, ctx); return Request.CreateResponse(HttpStatusCode.NoContent); } } } }
// PUT api/products/5 public virtual HttpResponseMessage Put(int id, Invoice invoice) { if (invoice == null || id != invoice.InvoiceId) { return Request.CreateResponse(HttpStatusCode.BadRequest); } else { using (AriUMContext ctx = new AriUMContext("AriUMDBConnection")) { // Does it exist? Invoice i = CntWebApiVerbs.GetInvoice(id, ctx); if (i == null) { return Request.CreateResponse(HttpStatusCode.NotFound); } else { i = CntWebApiVerbs.PutInvoice(invoice, ctx); FetchStrategy fs = new FetchStrategy(); fs.LoadWith<Invoice>(x => x.Customer); Invoice idt = ctx.CreateDetachedCopy<Invoice>(i, fs); return Request.CreateResponse<Invoice>(HttpStatusCode.OK, idt); } } } }