public static string UploadReport(HttpPostedFileBase File, string Title, string Description) { if (File == null || File.ContentLength <= 0 || string.IsNullOrEmpty(File.FileName)) return "err_03"; string UserId = UsersHelper.GetMyUserId(); string fileExtension = Path.GetExtension(File.FileName).ToLower(); // we accept only xlsx Excel files if (fileExtension != ".xlsx") return "no_xlsx"; string dataDir = HttpContext.Current.Server.MapPath("~/xls_reports/"), userDir = Path.Combine(dataDir, UserId); if (!Directory.Exists(userDir)) Directory.CreateDirectory(userDir); using (spark1Entities db = new spark1Entities()) { // generating new report id bool exists = true; string report_id = ""; while (exists) { report_id = Guid.NewGuid().ToString(); exists = db.ReportEntries.Where(re => re.EntryId == report_id).Count() > 0; } // adding the report to ReportEntries database table db.ReportEntries.Add(new ReportEntries() { EntryId = report_id, Description = Description, Title = Title, EntryDateTime = DateTime.Now, EntryUser = UserId }); try { // save changes to database db.SaveChanges(); } catch { return "err_01"; } try { // save file to user directory string fileName = Path.Combine(userDir, report_id + fileExtension); File.SaveAs(fileName); } catch { return "err_02"; } // set newly uploaded file as users default SetDefaultDBEntry(report_id); return report_id; } }
public static string UpdateLoginStats(string Username) { using (spark1Entities db = new spark1Entities()) { string reportId = null; IEnumerable<UserSettings> settings = db .UserSettings .Include("AspNetUsers") .Where(us => us.AspNetUsers.UserName == Username); if (settings != null && settings.Count() == 1) { settings.FirstOrDefault().LastLogin = DateTime.Now; reportId = settings.FirstOrDefault().SelectedReport; } else { string userId = db .AspNetUsers .Where(us => us.UserName == Username) .FirstOrDefault() .Id; reportId = db .ReportEntries .OrderByDescending(r => r.EntryDateTime) .FirstOrDefault() .EntryId; db .UserSettings .Add(new UserSettings() { UserId = userId, LastLogin = DateTime.Now, SelectedReport = reportId }); } try { db.SaveChanges(); } catch (Exception e) { return e.Message; } HttpContext.Current.Session["Data"] = DBReportHelper.ParseDataWorkSheet( DBReportHelper.GetUserDefaultDBEntry().EntryId); HttpContext.Current.Session["DataTitle"] = DBReportHelper.GetUserDefaultDBEntry(); return null; } }
// Store default DB Entry to database public static string SetDefaultDBEntry(string Report_id) { using (spark1Entities db = new spark1Entities()) { string User_id = UsersHelper.GetMyUserId(); // get user settings entry from UserSettings table in database IEnumerable<UserSettings> userSettings = db.UserSettings.Where(us => us.UserId == User_id); // if there is no entry for this user if (userSettings == null || userSettings.Count() <= 0) { // add new user settings entry db.UserSettings.Add(new UserSettings() { LastLogin = DateTime.Now, UserId = User_id, SelectedReport = Report_id }); } else { // change value in SelectedReport field in the UserSettings table userSettings.FirstOrDefault().SelectedReport = Report_id; } // save changes to the database try { db.SaveChanges(); } catch (Exception e) { return e.Message; } HttpContext.Current.Session["Data"] = ParseDataWorkSheet( GetUserDefaultDBEntry().EntryId); HttpContext.Current.Session["DataTitle"] = DBReportHelper.GetUserDefaultDBEntry(); return null; } }