protected void Save_Log(object sender, EventArgs e)
        {
            Int32 logId = 0;

            if (!String.IsNullOrEmpty(Request.QueryString["logId"]))
            {
                logId = Convert.ToInt32(Request.QueryString["logId"]);
            }

            var conn = new a2Entities();

            log l = new log();

            // Get current user id
            // http://stackoverflow.com/questions/20925822/asp-mvc5-identity-how-to-get-current-applicationuser
            string userId = User.Identity.GetUserId();
            var    user   = (new a2Entities()).AspNetUsers.FirstOrDefault(u => u.Id == userId);

            // Attach values to new object
            l.authorId = Convert.ToString(user);
            l.title    = title.Text;
            l.notes    = notes.Text;

            if (!String.IsNullOrEmpty(assets.SelectedValue))
            {
                l.assetId = Convert.ToInt32(assets.SelectedValue);
            }

            // Get asset related data
            if (!String.IsNullOrEmpty(l.assetId.ToString()))
            {
                var assetObj = (from a2 in conn.assets where a2.Id == l.assetId select a2).FirstOrDefault();
                l.assetCallSign = assetObj.callSign;
            }

            // Save logObj to db
            if (logId == 0)
            {
                // Get current datetime in EST format
                // http://stackoverflow.com/questions/5997570/how-to-convert-datetime-to-eastern-time
                var          timeUtc     = DateTime.UtcNow;
                TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
                l.createdDate = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, easternZone);

                conn.logs.Add(l);
            }
            // Update log
            else
            {
                l.Id = logId;
                conn.logs.Attach(l);
                conn.Entry(l).State = System.Data.Entity.EntityState.Modified;
                // Make sure created datetime does NOT get updated
                conn.Entry(l).Property(o => o.createdDate).IsModified = false;
            }

            // Save the log changes
            conn.SaveChanges();

            // Redirect to logs
            Response.Redirect(ResolveUrl("~/private/logs.aspx"));
        }
Exemplo n.º 2
0
        protected void Save_Asset(object sender, EventArgs e)
        {
            // Determine if asset is being updated or added
            Int32 assetId = 0;

            if (!String.IsNullOrEmpty(Request.QueryString["assetId"]))
            {
                assetId = Convert.ToInt32(Request.QueryString["assetId"]);
            }

            var conn = new a2Entities();

            asset a = new asset();

            // Get current user id
            // http://stackoverflow.com/questions/20925822/asp-mvc5-identity-how-to-get-current-applicationuser
            string userId = User.Identity.GetUserId();
            var    user   = (new a2Entities()).AspNetUsers.FirstOrDefault(s => s.Id == userId);

            // Attach values to new object
            a.authorId = Convert.ToString(user);
            a.callSign = call_sign.Text;

            if (!String.IsNullOrEmpty(year.SelectedValue))
            {
                a.year = Convert.ToInt32(year.SelectedValue);
            }

            a.brand = brand.Text;
            a.model = model.Text;
            a.vin   = vin.Text;

            // Make sure there is a value to convert before converting to avoid errors
            if (!String.IsNullOrEmpty(hours.Text))
            {
                a.hours = Convert.ToInt32(hours.Text);
            }

            a.odometerUnit = odometer_unit.SelectedValue;

            if (!String.IsNullOrEmpty(odometer.Text))
            {
                a.odometer = Convert.ToInt32(odometer.Text);
            }

            if (!String.IsNullOrEmpty(purchased_date.Text))
            {
                a.purchasedDate = DateTime.ParseExact(purchased_date.Text, "yyyy-MM-dd", null);
            }

            if (!String.IsNullOrEmpty(price_paid.Text))
            {
                a.pricePaid = Convert.ToDecimal(price_paid.Text);
            }

            a.notes = notes.Text;

            // Save assetObj to db
            if (assetId == 0)
            {
                // Get current datetime in EST format
                // http://stackoverflow.com/questions/5997570/how-to-convert-datetime-to-eastern-time
                var          timeUtc     = DateTime.UtcNow;
                TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
                a.createdDate = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, easternZone);

                conn.assets.Add(a);
            }
            // Update asset
            else
            {
                a.Id = assetId;
                conn.assets.Attach(a);
                conn.Entry(a).State = System.Data.Entity.EntityState.Modified;
                // Make sure created datetime does NOT get updated
                conn.Entry(a).Property(o => o.createdDate).IsModified = false;
            }

            // Save the asset
            conn.SaveChanges();

            // Redirect to assets
            Response.Redirect(ResolveUrl("~/private/assets.aspx"));
        }