Exemple #1
0
 public LotEditModel(Lot lot)
     : base()
 {
     Description = lot.Description;
     Price = lot.Price;
     Picture = lot.Picture;
     Name = lot.Name;
     Id = lot.Id;
     Customer_id = lot.Customer_id;
     Owner_id = lot.Owner_id;
     StartTime = lot.StartTime;
     DateEnd = lot.DateEnd;
     Category_id = lot.Category_id;
 }
        public ActionResult Delete(Lot review)
        {
            //  HttpUtility.HtmlEncode(id);
            Lot lot = lotrep.GetLot(review.Id);
            if (lot == null)
            {/// return RedirectToAction("Http404", "Error");
                throw new Exception("This lot doesn't exist!!!");
            }

            DeletePicture(lot.Picture);
            // послать сообщение об удалении
            User userOwner = new UserRepository().GetDBUser(lot.Owner_id);
            MailSender.SendMail(2, userOwner, lot);
            lotrep.DeleteLot(lot.Id);

            return RedirectToAction("Index", "Home");
        }
        public ActionResult Create(Lot newLot, IEnumerable<HttpPostedFileBase> fileUpload)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    LoadPicture(fileUpload, ref newLot);
                    // TODO: Add insert logic here
                    var user_id = new UserRepository().GetDBUser(User.Identity.Name).user_id;
                    newLot.Owner_id = user_id;
                    var review = new LotRepository().InsertLot(newLot);

                    return RedirectToAction("Index", "Home");
                }
                catch
                {
                    return View(newLot);
                }
            }
            return View(newLot);
        }
        private void LoadPicture(IEnumerable<HttpPostedFileBase> fileUpload, ref Lot lot)
        {
            // to do проверить существует ли лот с таким id
            if (fileUpload.FirstOrDefault() != null)
            {
                string[] mimeTypes = { "image/jpeg", "image/gif", "image/png", "image/bmp", "image/tiff", "image/x-jg", "image/x-icon", "image/ief", "image/pjpeg" };
                foreach (var file in fileUpload)
                {
                    if (file == null) continue;

                    string path = AppDomain.CurrentDomain.BaseDirectory + "UploadedFiles";
                    string filename = Path.GetFileName(file.FileName);
                    string type = file.ContentType;
                    bool isCorrect = false;
                    foreach (string mimeType in mimeTypes)
                    {
                        if (string.Equals(type, mimeType))
                        {
                            isCorrect = true;
                            break;
                        }
                    }

                    if (isCorrect)
                    {
                        //удаление старого
                        string picture = lot.Picture;
                        if (picture != null)
                        {
                            DeletePicture(picture);
                        }
                        // сохранение нового
                        string imgPath = "/UploadedFiles/" + filename;
                        lot.Picture = imgPath;
                        if (System.IO.File.Exists(filename))
                        {
                            filename = System.IO.Path.GetRandomFileName();
                        }
                        file.SaveAs(Path.Combine(path, filename));
                    }
                    else
                    {
                        throw new Exception("Type of file is incorrect! Required image file!!!");
                    }
                }
            }
        }
        public ActionResult Edit(Lot review, IEnumerable<HttpPostedFileBase> fileUpload)
        {
            if (TryUpdateModel(review))
            {
                LoadPicture(fileUpload, ref review);
                LotRepository lotrep = new LotRepository();
                lotrep.UpdateLot(review);

                return RedirectToAction("Index", "Home");
            }
            else
                return View(review);
        }
Exemple #6
0
        public static void SendMail(int messageType, User user, Lot lot = null)
        {
            Message message = GetMessage(messageType);
            string fullName = String.Concat(user.name, " ", user.lastName);
            switch (messageType)
            {
                //Registration
                #region Registration
                case 1:
                    {
                        string link = HttpContext.Current.Request.Url.Authority+"/Account/Activate/" +  user.userName + "/" + user.newMail;
                        var newMessage = new MailMessage(ConfigurationManager.AppSettings["MailName"], user.mail)
                        {
                            Subject = message.Subject,
                            Body = String.Format(message.Body, link) // 0-activation link
                        };

                        var client = new SmtpClient();
                        client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["MailName"], ConfigurationManager.AppSettings["MailPassword"]);
                        client.Send(newMessage);
                    }
                    break;
                #endregion
                #region Deleted lot
                case 2: // Deleted lot
                    {

                        var newMessage = new MailMessage(ConfigurationManager.AppSettings["MailName"], user.mail)
                        {
                            Subject = message.Subject,
                            Body = String.Format(message.Body, fullName, lot.Name + " (" + lot.Description + ")")
                        };

                        var client = new SmtpClient();
                        client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["MailName"], ConfigurationManager.AppSettings["MailPassword"]);
                        client.Send(newMessage);
                    }
                    break;
                    #endregion
                #region Deleted user
                case 3: //Deleted user
                    {
                        var newMessage = new MailMessage(ConfigurationManager.AppSettings["MailName"], user.mail)
                        {
                            Subject = message.Subject,
                            Body = String.Format(message.Body, fullName, user.userName)
                        };

                        var client = new SmtpClient();
                        client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["MailName"], ConfigurationManager.AppSettings["MailPassword"]);
                        client.Send(newMessage);
                    }
                    break;
                    #endregion
                #region To winner customer
                case 4: //To winner customer
                    {
                        var newMessage = new MailMessage(ConfigurationManager.AppSettings["MailName"], user.mail)
                        {
                            Subject = String.Format(message.Subject,lot.Name),
                            Body = String.Format(message.Body, fullName, lot.Name, lot.Price) // 0-
                        };

                        var client = new SmtpClient();
                        client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["MailName"], ConfigurationManager.AppSettings["MailPassword"]);
                        client.Send(newMessage);
                    }
                    break;
                    #endregion
                #region Broken bet to customer
                case 5: //Broken bet to customer
                    {
                        var newMessage = new MailMessage(ConfigurationManager.AppSettings["MailName"], user.mail)
                        {
                            Subject = message.Subject,
                            Body = String.Format(message.Body, fullName, lot.Price, lot.Name) // 0-
                        };

                        var client = new SmtpClient();
                        client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["MailName"], ConfigurationManager.AppSettings["MailPassword"]);
                        client.Send(newMessage);
                    }
                    break;
                    #endregion
                #region To owner successfull end of auction
                case 6:   // To owner successfull end of auction
                    {
                        var newMessage = new MailMessage(ConfigurationManager.AppSettings["MailName"], user.mail)
                        {
                            Subject = String.Format(message.Subject, lot.Name),
                            Body = String.Format(message.Body, fullName, lot.Name, lot.Price) // 0-
                        };

                        var client = new SmtpClient();
                        client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["MailName"], ConfigurationManager.AppSettings["MailPassword"]);
                        client.Send(newMessage);
                    }
                    break;
                    #endregion
                #region To owner auction failed
                case 7:   // To owner auction failed
                    {
                        var newMessage = new MailMessage(ConfigurationManager.AppSettings["MailName"], user.mail)
                        {
                            Subject = message.Subject,
                            Body = String.Format(message.Body, fullName, lot.Name+": "+ lot.Description) // 0-
                        };

                        var client = new SmtpClient();
                        client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["MailName"], ConfigurationManager.AppSettings["MailPassword"]);
                        client.Send(newMessage);
                    }
                    break;
                    #endregion
                #region Restore password
                case 8:
                    {
                        //string link = HttpContext.Current.Request.Url.Authority + "/Account/Activate/" + user.userName + "/" + user.newMail;
                        var newMessage = new MailMessage(ConfigurationManager.AppSettings["MailName"], user.mail)
                        {
                            Subject = String.Format(message.Subject, ConfigurationManager.AppSettings["SiteName"]),
                            Body = String.Format(message.Body, user.newMail) // 0-activation link
                        };

                        var client = new SmtpClient();
                        client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["MailName"], ConfigurationManager.AppSettings["MailPassword"]);
                        client.Send(newMessage);
                    }
                    break;
                #endregion
            }
        }
        public void UpdateLot(Lot lot)
        {
            string procedure = "P_ULot";
            SqlParameter lot_id = new SqlParameter("@lot_id", lot.Id);
            SqlParameter name = new SqlParameter("@name", lot.Name);
            SqlParameter description = new SqlParameter("@description", lot.Description);
            SqlParameter price = new SqlParameter("@price", lot.Price);
               // SqlParameter startTime = new SqlParameter("@startTime", lot.StartTime);
               // SqlParameter dateEnd = new SqlParameter("@dateEnd", lot.DateEnd);
            SqlParameter picture = new SqlParameter("@picture", lot.Picture);
               // SqlParameter owner_id = new SqlParameter("@owner_id", lot.Owner_id);
            SqlParameter category_id = new SqlParameter("@category_id", lot.Category_id);

            SqlParameter[] col_param = { lot_id, price, description, name, picture, category_id }; //, dateEnd, startTime , owner_id

            ADO_db.ExeProcedure(connString, procedure, col_param);
        }
        public bool InsertLot(Lot lot)
        {
            bool result = false;
            try
            {
                string procedure = "P_ILot";

                SqlParameter price = new SqlParameter("@price", lot.Price);
                SqlParameter description = new SqlParameter("@description", lot.Description);
                SqlParameter name = new SqlParameter("@name", lot.Name);
                SqlParameter picture = new SqlParameter("@picture", lot.Picture);
                SqlParameter category_id = new SqlParameter("@category_id", lot.Category_id);
                SqlParameter owner_id = new SqlParameter("@owner_id", lot.Owner_id);
                SqlParameter dateEnd = new SqlParameter("@dateEnd", lot.DateEnd);
                SqlParameter startTime = new SqlParameter("@startTime", lot.StartTime);
                SqlParameter[] col_param = { price, description, name, picture, category_id, owner_id, dateEnd, startTime };
                ADO_db.ExeProcedure(connString, procedure, col_param);
                result = true;
            }
            catch (Exception exc)
            {
                throw exc;
            }
            return result;
        }
        public List<Lot> GetLots(string qSelect, SqlParameter [] param)
        {
            List<Lot> lots = new List<Lot>();
            using (SqlConnection conn = new SqlConnection(connString))
            {
                SqlCommand cmd = new SqlCommand(qSelect, conn);
                cmd.Parameters.AddRange(param);
                cmd.Connection.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                using (reader)
                {
                    while (reader.Read())
                    {
                        Lot tmpLot = new Lot();
                        tmpLot.Id = (int)reader["lot_id"];

                        try
                        { tmpLot.Name = (string)reader["name"]; }
                        catch (InvalidCastException) { tmpLot.Name = "noName"; }

                        try
                        { tmpLot.Description = (string)reader["description"]; }
                        catch (InvalidCastException) { tmpLot.Description = "noDescription"; }

                        try
                        { tmpLot.Price = (double)reader["price"]; }
                        catch (InvalidCastException) { tmpLot.Price = 0; }

                        try
                        { tmpLot.Picture = (string)reader["picture"]; }
                        catch (InvalidCastException) { tmpLot.Picture = "/Content/images/noPicture.png"; }

                        try
                        { tmpLot.Owner_id = (int)reader["owner_id"]; }
                        catch (InvalidCastException) { tmpLot.Owner_id = 0; }

                        try
                        { tmpLot.Customer_id = (int)reader["customer_id"]; }
                        catch (InvalidCastException) { tmpLot.Customer_id = 0; }

                        try
                        { tmpLot.Category_id = (int)reader["category_id"]; }
                        catch (InvalidCastException) { tmpLot.Category_id = 0; }

                        try
                        { tmpLot.StartTime = (DateTime)reader["startTime"]; }
                        catch (InvalidCastException) { tmpLot.StartTime = DateTime.Now; }

                        try
                        { tmpLot.DateEnd = (DateTime)reader["dateEnd"]; }
                        catch (InvalidCastException) { tmpLot.DateEnd = DateTime.Now; }
                        lots.Add(tmpLot);
                    }
                }
                conn.Close();
            }
            return lots;
        }
        public Lot GetLot(int id)
        {
            string qSelect = "SELECT [name],[description],[price],[customer_id],[startTime],[dateEnd],[picture],[owner_id],[category_id] FROM [ASPNET_appDB].[dbo].[Lot] Where [lot_id]=@id";
            SqlParameter param = new SqlParameter("@id", id);

            Lot lot = new Lot();
            using (SqlConnection conn = new SqlConnection(connString))
            {
                SqlCommand cmd = new SqlCommand(qSelect, conn);
                cmd.Parameters.Add(param);
                cmd.Connection.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                //    lot = ReadLot(reader);
                //    return lot.ElementAt(0);
                //}
                #region Reader
                using (reader)
                {
                    while (reader.Read())
                    {

                        lot.Id = id;

                        try
                        { lot.Name = (string)reader["name"]; }
                        catch (InvalidCastException) { lot.Name = "noName"; }

                        try
                        { lot.Description = (string)reader["description"]; }
                        catch (InvalidCastException) { lot.Description = "noDescription"; }

                        try
                        { lot.Price = (double)reader["price"]; }
                        catch (InvalidCastException) { lot.Price = 0; }

                        try
                        { lot.Picture = (string)reader["picture"]; }
                        catch (InvalidCastException) { lot.Picture = "/Content/images/noImage.png"; }

                        try
                        { lot.Owner_id = (int)reader["owner_id"]; }
                        catch (InvalidCastException) { lot.Owner_id = 0; }

                        try
                        { lot.Customer_id = (int)reader["customer_id"]; }
                        catch (InvalidCastException) { lot.Customer_id = 0; }

                        try
                        { lot.Category_id = (int)reader["category_id"]; }
                        catch (InvalidCastException) { lot.Category_id = 0; }

                        try
                        { lot.StartTime = (DateTime)reader["startTime"]; }
                        catch (InvalidCastException) { lot.StartTime = DateTime.Now; }

                        try
                        { lot.DateEnd = (DateTime)reader["dateEnd"]; }
                        catch (InvalidCastException) { lot.DateEnd = DateTime.Now; }
                    }
                }
            }
            return lot;

                #endregion
        }