public HttpResponseMessage UpdateOrderStatus(string OitmID, [FromBody] Table_OrderDetails od)
        {
            try
            {
                string email = Thread.CurrentPrincipal.Identity.Name;
                using (OMSdbEntities entities = new OMSdbEntities())
                {
                    string AdminName = (from ad in entities.AppAdministrators where ad.AdminEmail == email select ad.AdminName).FirstOrDefault();
                    if (entities.AppAdministrators.Any(ad => ad.AdminEmail == email && ad.AdminName == AdminName || AdminName != null))
                    {
                        var entity = entities.Table_OrderDetails.FirstOrDefault(e => e.OrderItemID == OitmID && e.OrderStatus != "Completed");
                        entity.OrderStatus = od.OrderStatus;
                        entities.SaveChanges();

                        var message = Request.CreateResponse(HttpStatusCode.Created);
                        return(message);
                    }
                    else
                    {
                        var message = Request.CreateResponse(HttpStatusCode.Unauthorized);
                        return(message);
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Example #2
0
        public HttpResponseMessage UpdateOrderQuantity(string oItmID, int qtty)
        {
            try
            {
                string email = Thread.CurrentPrincipal.Identity.Name;
                using (OMSdbEntities entities = new OMSdbEntities())
                {
                    Table_OrderDetails od        = new Table_OrderDetails();
                    string             BuyerID   = (from b in entities.Buyers where b.Email == email select b.BuyerID).FirstOrDefault();
                    string             BuyerName = (from b in entities.Buyers where b.Email == email select b.BuyerName).FirstOrDefault();
                    if (entities.Buyers.Any(buyer => buyer.BuyerID.Equals(BuyerID) && buyer.BuyerName.Equals(BuyerName) || BuyerID != null || BuyerName != null))
                    {
                        if (entities.Table_OrderDetails.Any(odrs => odrs.OrderItemID.Equals(oItmID) && odrs.BuyerID.Equals(BuyerID)))
                        {
                            Nullable <int> CurrentOrderQtty = (from ord in entities.Table_OrderDetails where ord.OrderItemID.Equals(oItmID) && ord.OrderStatus != "Completed" select ord.OrderQuantity).FirstOrDefault();
                            string         ProductID        = (from pr in entities.Table_OrderDetails where pr.OrderItemID.Equals(oItmID) select pr.ProductID).FirstOrDefault();
                            var            entiti           = entities.Table_OrderDetails.FirstOrDefault(e => e.OrderItemID.Equals(oItmID) && e.OrderStatus != "Completed");
                            entiti.OrderQuantity = CurrentOrderQtty + (qtty);
                            Nullable <int> avilableQuantity = (from p in entities.Table_ProductsDetails where p.ProductID == ProductID select p.AvailableQuantity).FirstOrDefault();
                            var            proEntity        = entities.Table_ProductsDetails.FirstOrDefault(e => e.ProductID.Equals(ProductID));
                            proEntity.AvailableQuantity = avilableQuantity + (-qtty);
                            entities.SaveChanges();

                            string msg = "quantity Updated Successfully";
                            return(Request.CreateResponse(HttpStatusCode.Accepted, msg));
                        }
                        else
                        {
                            string msg = "Either The Product has not been ordered yet OR you are unautharized To update the Order";
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, msg));
                        }
                    }
                    else
                    {
                        var message = Request.CreateResponse(HttpStatusCode.Unauthorized);
                        return(message);
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Example #3
0
        public HttpResponseMessage ADDbuyers([FromBody] Buyer buyer)
        {
            try
            {
                using (OMSdbEntities entities = new OMSdbEntities())
                {
                    entities.Buyers.Add(buyer);
                    entities.SaveChanges();

                    var message = Request.CreateResponse(HttpStatusCode.Created, buyer);
                    message.Headers.Location = new Uri(Request.RequestUri + buyer.ID.ToString());
                    return(message);
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Example #4
0
        public HttpResponseMessage DeleteOrder(string oItmID)
        {
            try
            {
                string email = Thread.CurrentPrincipal.Identity.Name;
                using (OMSdbEntities entities = new OMSdbEntities())
                {
                    string BuyerID   = (from b in entities.Buyers where b.Email == email select b.BuyerID).FirstOrDefault();
                    string BuyerName = (from b in entities.Buyers where b.Email == email select b.BuyerName).FirstOrDefault();
                    if (entities.Buyers.Any(buyer => buyer.BuyerID.Equals(BuyerID) && buyer.BuyerName.Equals(BuyerName) || BuyerID != null || BuyerName != null))
                    {
                        if (entities.Table_OrderDetails.Any(order => order.BuyerID.Equals(BuyerID) && order.OrderItemID.Equals(oItmID)))
                        {
                            Nullable <int> OrderQuantity            = (from ods in entities.Table_OrderDetails where ods.OrderItemID == oItmID select ods.OrderQuantity).FirstOrDefault();
                            string         productID                = (from ods in entities.Table_OrderDetails where ods.OrderItemID == oItmID select ods.ProductID).FirstOrDefault();
                            Nullable <int> AvailableProductQuantity = (from pro in entities.Table_ProductsDetails where pro.ProductID == productID select pro.AvailableQuantity).FirstOrDefault();
                            entities.Table_OrderDetails.Remove(entities.Table_OrderDetails.FirstOrDefault(odr => odr.OrderItemID == oItmID));
                            var entity = entities.Table_ProductsDetails.FirstOrDefault(p => p.ProductID == productID);
                            entity.AvailableQuantity = AvailableProductQuantity + OrderQuantity;
                            entities.SaveChanges();

                            var message = "Order with OrderItemID " + oItmID + " Has been Deleted Successfully";
                            return(Request.CreateResponse(HttpStatusCode.OK, message));
                        }
                        else
                        {
                            var message = Request.CreateResponse(HttpStatusCode.Unauthorized);
                            return(message);
                        }
                    }
                    else
                    {
                        var message = Request.CreateResponse(HttpStatusCode.Unauthorized);
                        return(message);
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
        public HttpResponseMessage ADDProduct(string proID, [FromBody] Table_ProductsDetails Pro)
        {
            try
            {
                string email = Thread.CurrentPrincipal.Identity.Name;
                using (OMSdbEntities entities = new OMSdbEntities())
                {
                    string AdminName = (from ad in entities.AppAdministrators where ad.AdminEmail == email select ad.AdminName).FirstOrDefault();
                    if (entities.AppAdministrators.Any(ad => ad.AdminEmail == email && ad.AdminName == AdminName || AdminName != null))
                    {
                        if (entities.Table_ProductsDetails.Any(pr => pr.ProductID.Equals(proID)))
                        {
                            string mesge = "Product ID Exists";
                            return(Request.CreateResponse(HttpStatusCode.Conflict, mesge));
                        }
                        else
                        {
                            Pro.ProductID = proID;
                            entities.Table_ProductsDetails.Add(Pro);
                            entities.SaveChanges();

                            var message = Request.CreateResponse(HttpStatusCode.Created, Pro);
                            message.Headers.Location = new Uri(Request.RequestUri + Pro.ID.ToString());
                            return(message);
                        }
                    }
                    else
                    {
                        var message = Request.CreateResponse(HttpStatusCode.Unauthorized);
                        return(message);
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Example #6
0
        public HttpResponseMessage PlaceOrder(string ProID, int ordQty)
        {
            try
            {
                string email = Thread.CurrentPrincipal.Identity.Name;
                using (OMSdbEntities entities = new OMSdbEntities())
                {
                    Table_OrderDetails od        = new Table_OrderDetails();
                    string             BuyerID   = (from b in entities.Buyers where b.Email == email select b.BuyerID).FirstOrDefault();
                    string             BuyerName = (from b in entities.Buyers where b.Email == email select b.BuyerName).FirstOrDefault();
                    if (entities.Buyers.Any(buyer => buyer.BuyerID.Equals(BuyerID) && buyer.BuyerName.Equals(BuyerName) || BuyerID != null || BuyerName != null))
                    {
                        if (entities.Table_ProductsDetails.Any(pro => pro.ProductID.Equals(ProID)))
                        {
                            od.OrderID     = "OD" + BuyerID;
                            od.OrderItemID = "OD" + BuyerID + ProID;
                            if (entities.Table_OrderDetails.Any(odrs => odrs.OrderItemID.Equals(od.OrderItemID) && odrs.OrderStatus != "Completed"))
                            {
                                string messg = "Product Already present in Previous Order Please Update Your Previous Order";
                                return(Request.CreateResponse(HttpStatusCode.Ambiguous, messg));
                            }
                            else
                            {
                                od.BuyerEmail    = email;
                                od.BuyerID       = BuyerID;
                                od.OrderQuantity = ordQty;
                                od.ProductID     = ProID;
                                od.OrderStatus   = "Placed";
                                entities.Table_OrderDetails.Add(od);
                            }
                        }
                        else
                        {
                            string msg = "Product Not Found";
                            return(Request.CreateResponse(HttpStatusCode.NotFound, msg));
                        }


                        Nullable <int> avilableQuantity = (from p in entities.Table_ProductsDetails where p.ProductID == ProID select p.AvailableQuantity).FirstOrDefault();
                        var            entity           = entities.Table_ProductsDetails.FirstOrDefault(p => p.ProductID == ProID);
                        entity.AvailableQuantity = avilableQuantity - ordQty;
                        entities.SaveChanges();

                        //Sending EMail to gmail

                        MailMessage mm = new MailMessage();
                        mm.From = new MailAddress("*****@*****.**");
                        mm.To.Add(email);
                        mm.Subject    = "OMS Order";
                        mm.Body       = "Your Order has been successfully placed ";
                        mm.IsBodyHtml = false;
                        SmtpClient smtp = new SmtpClient("smtp.gmail.com");
                        smtp.UseDefaultCredentials = true;
                        smtp.Port        = 587;
                        smtp.EnableSsl   = true;
                        smtp.Credentials = new System.Net.NetworkCredential("*****@*****.**", "doitb4udie");
                        smtp.Send(mm);


                        var message = Request.CreateResponse(HttpStatusCode.Created, od);
                        message.Headers.Location = new Uri(Request.RequestUri + od.OrderID.ToString());
                        return(message);
                    }
                    else
                    {
                        var message = Request.CreateResponse(HttpStatusCode.Unauthorized);
                        return(message);
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
        public Task <HttpResponseMessage> UploadProductImage(string PID)
        {
            string email = Thread.CurrentPrincipal.Identity.Name;

            using (OMSdbEntities entities = new OMSdbEntities())
            {
                string AdminName = (from ad in entities.AppAdministrators where ad.AdminEmail == email select ad.AdminName).FirstOrDefault();
                if (entities.AppAdministrators.Any(ad => ad.AdminEmail == email && ad.AdminName == AdminName || AdminName != null))
                {
                    if (entities.Table_ProductsDetails.Any(pro => pro.ProductID.Equals(PID)))
                    {
                        List <string> savedFilePath = new List <string>();
                        // Check if the request contains multipart/form-data
                        if (!Request.Content.IsMimeMultipartContent())
                        {
                            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                        }
                        //Get the path of folder where we want to upload all files.
                        string rootPath = HttpContext.Current.Server.MapPath("~/ProductImages");
                        var    provider = new MultipartFileStreamProvider(rootPath);
                        // Read the form data.
                        //If any error(Cancelled or any fault) occurred during file read , return internal server error
                        var task = Request.Content.ReadAsMultipartAsync(provider).
                                   ContinueWith <HttpResponseMessage>(t =>
                        {
                            if (t.IsCanceled || t.IsFaulted)
                            {
                                Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
                            }
                            foreach (MultipartFileData dataitem in provider.FileData)
                            {
                                try
                                {
                                    //Replace / from file name
                                    string name = dataitem.Headers.ContentDisposition.FileName.Replace("\"", "");

                                    //Fetching File name without extension
                                    string[] fullname = name.Split('.');
                                    string fname      = fullname[0];

                                    string newFileName = fname + PID + Path.GetExtension(name);
                                    //Move file from current location to target folder.
                                    File.Move(dataitem.LocalFileName, Path.Combine(rootPath, newFileName));
                                }
                                catch (Exception ex)
                                {
                                    string message = ex.Message;
                                }
                            }

                            return(Request.CreateResponse(HttpStatusCode.Created, savedFilePath));
                        });
                        foreach (MultipartFileData dataitem in provider.FileData)
                        {
                            try
                            {
                                //Replace / from file name
                                string name = dataitem.Headers.ContentDisposition.FileName.Replace("\"", "");

                                string[] fullname = name.Split('.');
                                string   fname    = fullname[0];

                                string newFileName = fname + PID + Path.GetExtension(name);

                                Table_ProductImage pim = new Table_ProductImage();
                                pim.ProductID    = PID;
                                pim.ProductImage = "~/ProductImages/" + newFileName;
                                entities.Table_ProductImage.Add(pim);
                                entities.SaveChanges();
                            }
                            catch (Exception ex)
                            {
                                string message = ex.Message;
                            }
                        }
                        return(task);
                    }
                    else
                    {
                        var message = Request.CreateResponse(HttpStatusCode.NotFound);
                        return(Task.FromResult <HttpResponseMessage>(message));
                    }
                }
                else
                {
                    var message = Request.CreateResponse(HttpStatusCode.Unauthorized);
                    return(Task.FromResult <HttpResponseMessage>(message));
                }
            }
        }