public static BookingView CopyLineToNextLine(int currentBookingID, string connectionString)
        {
            AutoMapper.Mapper.CreateMap <Booking, BookingView>();
            try
            {
                using (CarolineCottageDbContext dbContext = new CarolineCottageDbContext(connectionString))
                {
                    Booking currentBooking = dbContext.Bookings.Find(currentBookingID);
                    Booking nextBooking    = dbContext.Bookings.OrderBy(x => x.WeekStartDate).Where(x => x.WeekStartDate > currentBooking.WeekStartDate).FirstOrDefault();

                    nextBooking.BookingStatus           = currentBooking.BookingStatus;
                    nextBooking.AvailableForShortBreaks = currentBooking.AvailableForShortBreaks;
                    nextBooking.Comment   = currentBooking.Comment;
                    nextBooking.WeekPrice = currentBooking.WeekPrice;

                    dbContext.Entry(nextBooking).State = EntityState.Modified;
                    dbContext.SaveChanges();

                    DateTime lastDate    = dbContext.Bookings.Max(x => x.WeekStartDate);
                    var      bookingView = AutoMapper.Mapper.Map <Booking, BookingView>(nextBooking);
                    bookingView.IsLastRow = (lastDate - bookingView.WeekStartDate).Days == 0;
                    return(bookingView);
                }
            }
            catch (Exception e)
            {
                return(new BookingView());
            }
        }
 public static UserView GetUserByID(int userID, string connectionString)
 {
     AutoMapper.Mapper.CreateMap <User, UserView>();
     using (CarolineCottageDbContext dbContext = new CarolineCottageDbContext(connectionString))
     {
         User user = dbContext.Users.FirstOrDefault(x => x.UserID == userID) ?? new User();
         return(AutoMapper.Mapper.Map <User, UserView>(user));
     }
 }
 public static BookingView GetBookingByID(int bookingID, string connectionString)
 {
     using (CarolineCottageDbContext dbContext = new CarolineCottageDbContext(connectionString))
     {
         DateTime lastDate    = dbContext.Bookings.Max(x => x.WeekStartDate);
         var      bookingView = AutoMapper.Mapper.Map <Booking, BookingView>(dbContext.Bookings.FirstOrDefault(x => x.BookingID == bookingID)) ?? new BookingView();
         bookingView.IsLastRow = (lastDate - bookingView.WeekStartDate).Days == 0;
         return(bookingView);
     }
 }
        public static List <UserList> GetUserList(string connectionString)
        {
            List <UserList> userList = new List <UserList>();

            AutoMapper.Mapper.CreateMap <User, UserList>();
            using (CarolineCottageDbContext dbContext = new CarolineCottageDbContext(connectionString))
            {
                userList = AutoMapper.Mapper.Map <List <User>, List <UserList> >(dbContext.Users.OrderBy(x => x.Name).ToList());
            }
            return(userList);
        }
 public static PasswordStatus GetPasswordStatus(string name, string password2Check, string connectionString)
 {
     using (CarolineCottageDbContext dbContext = new CarolineCottageDbContext(connectionString))
     {
         var user = dbContext.Users.FirstOrDefault(x => x.Name == name) ?? new Repository.CarolineCottageClasses.User();
         if (PasswordUtilityFunctions.CreatePasswordHashSHA1(password2Check, user.Salt) == user.PasswordEnc)
         {
             return(PasswordStatus.Valid);
         }
     }
     return(PasswordStatus.Invalid);
 }
 public bool Save(string connectionString)
 {
     AutoMapper.Mapper.CreateMap <UserView, User>();
     using (CarolineCottageDbContext dbContext = new CarolineCottageDbContext(connectionString))
     {
         DateSet     = DateTime.Now;
         Salt        = PasswordUtilityFunctions.CreateSalt(6);
         PasswordEnc = PasswordUtilityFunctions.CreatePasswordHashSHA1(Password, Salt);
         dbContext.Entry(AutoMapper.Mapper.Map <UserView, User>(this)).State = this.UserID == 0 ? EntityState.Added : EntityState.Modified;
         dbContext.SaveChanges();
     }
     return(true);
 }
        public static BookingViewReturn GetCurrentBookings(string connectionString, bool addNewRows, DateTime endDateForDisplay, bool debugSQLConnection)
        {
            DateTime nextWeek = DateTimeExtensions.NextDayOfWeek(DateTime.Now, Booking.ChangeoverDay);

            Mapper.CreateMap <Booking, BookingView>();
            Mapper.CreateMap <BookingView, Booking>();
            BookingViewReturn bookingViewReturn = new BookingViewReturn();

            try
            {
                using (CarolineCottageDbContext dbContext = new CarolineCottageDbContext(connectionString))
                {
                    if (addNewRows)
                    {
                        //  get the latest date in the database
                        DateTime lastWeekStored = dbContext.Bookings.OrderByDescending(x => x.WeekStartDate).FirstOrDefault()?.WeekStartDate ?? nextWeek;

                        //  add in any extra to make up to at least a year's worth
                        DateTime endDate = nextWeek.AddDays(78 * 7);
                        for (DateTime weekDate = lastWeekStored.AddDays(7); (endDate - weekDate).TotalDays > 7; weekDate = weekDate.AddDays(7))
                        {
                            dbContext.Bookings.Add(AutoMapper.Mapper.Map <BookingView, Booking>(new BookingView(weekDate)));
                        }

                        dbContext.SaveChanges();
                    }
                    //  then get list
                    List <BookingView> currentBookings = AutoMapper.Mapper.Map <IEnumerable <Booking>, List <BookingView> >(dbContext.Bookings.Where(x => x.WeekStartDate >= nextWeek));

                    if (!addNewRows)
                    {
                        currentBookings = currentBookings.Where(x => x.WeekStartDate < endDateForDisplay).ToList();
                    }

                    currentBookings.Last().IsLastRow = true;
                    bookingViewReturn.BookingList = currentBookings;
                    return(bookingViewReturn);
                }
            }
            catch (Exception e)
            {
                bookingViewReturn.ReturnError = e.Message;
                if (!debugSQLConnection)
                {
                    bookingViewReturn.ReturnError = "List nor currently available";
                }
                return(bookingViewReturn);
            }
        }
        public void Save(string connectionString)
        {
            Mapper.CreateMap <BookingView, Booking>();

            try
            {
                using (CarolineCottageDbContext dbContext = new CarolineCottageDbContext(connectionString))
                {
                    dbContext.Entry(AutoMapper.Mapper.Map <BookingView, Booking>(this)).State = EntityState.Modified;
                    dbContext.SaveChanges();
                }
            }
            catch (Exception e)
            {
                //   flag the error to the controller
                BookingID = 0;
            }
        }