Пример #1
0
        /// <summary>
        /// Lägger till en ny boende, Behöver lägga till logik för att säkerställa att epost är unik
        /// </summary>
        /// <param name="adress">Ange adressen</param>
        /// <param name="email">Ange mailadressen</param>
        /// <param name="name">Ange Namnet</param>
        /// <returns></returns>
        public static Resident AddResident(string adress, string email, string name)
        {
            Resident newResident = null;
            using (var db = new WasherContext())
            {

                newResident = db.Residents.Add(
                    new Resident
                    {
                        Name = name,
                        Email = email,
                        Adress = adress
                    });
                try
                {
                    db.SaveChanges();
                }
                catch (Exception exception)
                {
                    return newResident;
                }
            }

            return newResident;
        }
Пример #2
0
        public ActionResult Index()
        {
            var model = new HomeViewModel();

            using (var db = new WasherContext())
            {
                CultureInfo swedish = new CultureInfo("sv-SV");
                var today = DateTime.Now;

                var d = db.Reservation.Where(x => !x.Reservationlist.Any()).ToList();
                d = d.Where(x => x.StarTime > today).Where(x => x.StarTime.DayOfYear == today.DayOfYear).ToList();

                model.TodaysFreeReservations = d;
                model.textforReservation = new List<string>();
                model.NameofCurrentday = UpperFirst(swedish.DateTimeFormat.DayNames[(int) today.DayOfWeek]);
                foreach (var item in d)
                {
                   model.textforReservation.Add($"kl: {item.StarTime.Hour}:{item.StarTime.Minute}0 - {item.EndTime.Hour}:{item.EndTime.Minute}0");
                }
                //.FirstOrDefault(x => SqlFunctions.DatePart("dayofyear", x.StarTime) > SqlFunctions.DatePart("dayofyear", SqlFunctions.CurrentTimestamp()));
                //DbFunctions.CreateDateTime(x.StarTime.Year, x.StarTime.Month, x.StarTime.Day, x.StarTime.Hour, x.StarTime.Minute, x.StarTime.Minute) > today);

            }

            return View(model);
        }
        // GET: ReservationView
        public ActionResult Index()
        {
            var model = new ReservationView();
            using (var db = new WasherContext())
            {

                model.ViewReservation = db.Reservation
                    .Include(x => x.Reservationlist)
                    .FirstOrDefault(x => x.Id == "2016-01-01 08");
                model.ViewResidents = db.Residents.ToList();
                model.selectResidentsList = new SelectList(db.Residents.ToList(), "ResidentId", "Name");
                ViewBag.Title = "Bokning";

            }
            return View(model);
        }
Пример #4
0
        public ActionResult ErrorMessage(int id)
        {
            var model = new ErrorViewModel();
            using (var db = new WasherContext())
            {
                var machine = db.Machines.FirstOrDefault(x => x.Id == id);

                if (machine == null) return RedirectToAction("Index");

                model.DescriptionText = "Beskriv vad det är som är fel";
                model.Machine = machine;
                model.SelectResidentsList = new SelectList(db.Residents?.ToList(), "ResidentId", "Name");

                return View(model);
            }
                
            
        }
        public ActionResult Index( FormCollection reservation)
        {
            if (ModelState.IsValid)
            {
                using (var db1 = new WasherContext())
                {
                    string id = reservation["ViewReservation.Id"];
                    int residentId = int.Parse(reservation["selectedResidentId"]);
                    var orgReservation = db1.Reservation.FirstOrDefault(x => x.Id == id );
                    var resident = db1.Residents.FirstOrDefault(x => x.ResidentId == residentId);
                    orgReservation.Reservationlist[0] = resident;

                    //var orginalReservation = db1.Reservation.Find(reservation.ViewReservation.Id);
                    //var resident = db1.Residents.Find();
                    //if (orginalReservation == null)
                    //{
                    //    return HttpNotFound();
                    //}

                    //orginalReservation.Reservationlist[0] = new Resident();
                    //orginalResident.Email = resident.Email;
                    //orginalResident.Adress = resident.Adress;
                    //orginalResident.Name = resident.Name;

                    db1.Entry(orgReservation).State = EntityState.Modified;
                    try
                    {
                        db1.SaveChanges();
                    }
                    catch (Exception)
                    {

                        return HttpNotFound();
                    }
                }

                return RedirectToAction("Index", "Reservations");
            }
            return View();
        }
Пример #6
0
        /// <summary>
        /// Hämtar en list med reservationer för en resident
        /// </summary>
        /// <param name="residentId"></param>
        /// <returns></returns>
        public static List<IList<Reservation>> GetResidentsReservations(int residentId)
        {
            List<IList<Reservation>> listOfReservations;
            using (var db = new WasherContext())
            {
                listOfReservations = db.Residents
                    .Where(x => x.ResidentId == residentId)
                    .Include(x => x.Reservations)
                    .Select(x => x.Reservations)
                    .ToList();

            }
            return listOfReservations;
        }
Пример #7
0
        /// <summary>
        /// Hämtar nästa 10 lediga tider på helg
        /// </summary>
        /// <returns></returns>
        public static List<Reservation> GetNextTenAvailbelReservations()
        {
            List<Reservation> reservations;
            using (var db = new WasherContext())
            {
                // Tar reda på söndag i dåtid och dagens datum, sedan använder diffen för att
                // kolla viken veckodag det är
                var today = DateTime.Now;
                var sunday = new DateTime(2014, 12, 7);

                reservations = db.Reservation
                // tar bara med om det inte finns någon reservation
                .Where(x => !x.Reservationlist.Any())
                // Kollar om det är Lördag eller söndag
                .Where(r => r.StarTime > today)
                .Where(r => DbFunctions.DiffDays(sunday, r.StarTime) % 7 == 6 || DbFunctions.DiffDays(sunday, r.StarTime) % 7 == 0)
                // Säkerställer att det i datumordning
                .OrderBy(x => x.StarTime)
                // Väljer det 10 första
                .Take(10)
                .ToList();

            }
            return reservations;
        }
Пример #8
0
        /// <summary>
        /// Hämtar nästa lediga tid
        /// </summary>
        /// <returns></returns>
        public static Reservation GetNextAvailbelReservations()
        {
            Reservation reservation;
            using (var db = new WasherContext())
            {
                var today = DateTime.Now;

                reservation = db.Reservation
                    .Where(x => x.ResidentId == 0)
                    .FirstOrDefault(x => DbFunctions.CreateDateTime(x.StarTime.Year, x.StarTime.Month, x.StarTime.Day, x.StarTime.Hour, x.StarTime.Minute, x.StarTime.Minute) > today);
            }
            return reservation;
        }
Пример #9
0
 /// <summary>
 /// Hämtar alla maskiner
 /// </summary>
 /// <returns>Returnerar en lista med alla maskien</returns>
 public static List<Machine> GetMachines()
 {
     using (var db = new WasherContext())
     {
         var listOfMachines = db.Machines.ToList();
         return listOfMachines;
     }
 }
Пример #10
0
 /// <summary>
 /// Hämtar lediga tider för en specifik dag
 /// </summary>
 /// <param name="date">Ange en giltig DateTime</param>
 /// <returns>Lista med Reservations</returns>
 public static List<Reservation> GetAvailbelReservations(string date)
 {
     List<Reservation> reservations;
     using (var db = new WasherContext())
     {
         DateTime today;
         DateTime.TryParse(date, out today);
         reservations = db.Reservation
              .Where(x => SqlFunctions.DatePart("Day", x.Id) == SqlFunctions.DatePart("Day", date) && !x.Reservationlist.Any())
              //.OrderBy()
              .ToList();
     }
     return reservations;
 }
Пример #11
0
        /// <summary>
        /// Avboka en reservation
        /// </summary>
        /// <param name="reservationId"></param>
        /// <param name="residentId"></param>
        /// <returns>Resravation som blev ändrad</returns>
        public static DeleteResult CancelReservation(string reservationId, int residentId)
        {
            var result = new DeleteResult();
            using (var db = new WasherContext())
            {
                db.Configuration.LazyLoadingEnabled = false;

                var reservation = db.Reservation.Include(l => l.Reservationlist).FirstOrDefault(r => r.Id == reservationId);

                if (reservation == null) return result;

                reservation?.Reservationlist.Remove(reservation?.Reservationlist[0]);

                if (reservation.Reservationlist.Any()) reservation.ResidentId = reservation.Reservationlist[0].ResidentId;

                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.Message = ex.Message;
                    return result;
                }
                result.Succes = true;

                if (result.Succes)
                {
                    SendMessage( reservation.Reservationlist[0]);
                }
            }

            return result;
        }
Пример #12
0
        /// <summary>
        /// Hämtar nästa 50 lediga tider 
        /// </summary>
        /// <returns></returns>
        public static List<ReservationApiResponse> ApiGetAvailbelReservations()
        {
            List<ReservationApiResponse> reservations;
            using (var db = new WasherContext())
            {
                db.Configuration.ProxyCreationEnabled = false;

                var today = DateTime.Now;

               reservations  = db.Reservation
                .Where(x => !x.Reservationlist.Any())
                .Where(x => x.StarTime > today)
                .OrderBy(x => x.StarTime)
                .Take(50)
                .Select(x => new ReservationApiResponse { Id = x.Id, StarTime = x.StarTime, EndTime = x.EndTime})
                .ToList();
            }
            return reservations;
        }
Пример #13
0
        // GET: Residents
        public ActionResult Index()
        {
            List<Resident> residents = null;
            using (var db1 = new WasherContext())
            {
               residents = db1.Residents.ToList();
            }

                return View(residents);
        }
Пример #14
0
        public ActionResult Edit([Bind(Include = "ResidentId,Email,Name,Adress,RowVersion")] Resident resident)
        {
            if (ModelState.IsValid)
            {
                using (var db1 = new WasherContext())
                {
                    var orginalResident = db1.Residents.Find(resident.ResidentId);
                    if (orginalResident == null)
                    {
                        return HttpNotFound();
                    }

                    orginalResident.Email = resident.Email;
                    orginalResident.Adress = resident.Adress;
                    orginalResident.Name = resident.Name;

                    db1.Entry(orginalResident).State = EntityState.Modified;
                    try
                    {
                        db1.SaveChanges();
                    }
                    catch (Exception ex)
                    {

                        var sdf = ex;
                        return HttpNotFound();
                    }
                }

                return RedirectToAction("Index");
            }
            return View(resident);
        }
        // GET: Reservations/Edit/5
        public ActionResult Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var model = new ReservationView();
            using (var db = new WasherContext())
            {
                
                model.ViewReservation = db.Reservation
                    .Include(x => x.Reservationlist)
                    .FirstOrDefault(x => x.Id == id);
                model.ViewResidents = db.Residents.ToList();
                model.selectResidentsList = new SelectList(db.Residents.ToList(), "ResidentId", "Name");
                ViewBag.Title = "Bokning";

            }
            return View(model);
        }
Пример #16
0
        /// <summary>
        /// Ställa sig i kö för en bokning
        /// </summary>
        /// <param name="residentId"></param>
        /// <param name="reservationsId"></param>
        /// <returns></returns>
        public static MethodResult MakeQueReservation(int residentId, string reservationsId, byte[] rowversion)
        {
            var result = new MethodResult();
            using (var db = new WasherContext())
            {
                var x = db.Reservation.FirstOrDefault(r => r.Id == reservationsId);
                if (x == null) return result;

                if (!x.Reservationlist.Any())
                {
                    result.Succes = false;
                    result.Message = "Du kan inte ställa dig i kö på en bokning som inte är bokad";
                    return result;

                }

                var residentToAdd = db.Residents.FirstOrDefault(a => a.ResidentId == residentId);

                x.Reservationlist.Add(residentToAdd);
                db.Entry(x).OriginalValues["RowVersion"] = rowversion;
                db.Entry(x).State = EntityState.Modified;
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.Succes = false;
                    result.Message = ex.Message;
                    return result;

                }

                return result;
            }
        }
Пример #17
0
        /// <summary>
        /// Skapar en bokning
        /// För att ställa sig i kö behövs det läggas till mer kod     
        /// </summary>
        /// <param name="residentId"></param>
        /// <param name="reservationsId"></param>
        /// <returns></returns>
        public static MethodResult MakeReservation(int residentId, string reservationsId, byte[] rowversion)
        {
            var result = new MethodResult();
            using (var db = new WasherContext())
            {
                var x = db.Reservation.FirstOrDefault(r => r.Id == reservationsId);
                if (x != null)
                {

                    if (x.ResidentId != 0)
                    {
                        return result;

                    }

                    x.ResidentId = residentId;
                    x.RowVersion = rowversion;
                    var residentToAdd = db.Residents.FirstOrDefault(a => a.ResidentId == residentId);

                    var existingreservations = residentToAdd?.Reservations
                        .Where(r => r.StarTime > DateTime.Now);

                    //if (existingreservations?.Count() > 2)
                    //{

                    //    return result;
                    //}
                    x.Reservationlist.Add(residentToAdd);
                    db.Entry(x).OriginalValues["RowVersion"] = rowversion;
                    db.Entry(x).State = EntityState.Modified;
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {

                        result.ConcurrEx = true;
                        result.Message = "Någon annan han före! Looser!";
                        return result;
                    }
                    result.Succes = true;
                    return result;
                }
                result.Succes = true;
                return result;
            }
        }
Пример #18
0
        /// <summary>
        /// Felanmäla ett maskin
        /// </summary>
        /// <param name="residentId">Ange ResidentID</param>
        /// <param name="errorDescription">Skriv en beskrivning av felet</param>
        /// <param name="machineId">Ange Machine ID</param>
        /// <returns>Returnera</returns>
        public static MethodResult CreateErrorMessage(int residentId, string errorDescription, int machineId)
        {
            var mr = new MethodResult();
            var em = new ErrorMessage();

            using (var db = new WasherContext())
            {
                em.ErrorDescription = errorDescription;
                em.ResidentId = residentId;
                em.Machine_Id = machineId;
                em.Time = DateTime.Now;

                db.ErrorMessages.Add(em);

                try
                {
                    db.SaveChanges();
                }
                catch (Exception)
                {

                    throw;
                }
            }
            mr.Object = em;
            mr.Succes = true;
            return mr;
        }
Пример #19
0
        /// <summary>
        /// Uppdatera en resident
        /// </summary>
        /// <param name="resident"></param>
        /// <returns>Den uppdaterade residenten</returns>
        public static Resident AlterResident(Resident resident)
        {
            Resident updatedResident = null;
            using (var db = new WasherContext())
            {
                var orginalResident = db.Residents.Find(resident.ResidentId);
                if (orginalResident == null)
                {
                    return updatedResident;
                }

                orginalResident.Email = resident.Email;
                orginalResident.Adress = resident.Adress;
                orginalResident.Name = resident.Name;

                db.Entry(orginalResident).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (Exception)
                {
                    throw;
                }
            }

            return resident;
        }
Пример #20
0
        /// <summary>
        /// Radera en Resident
        /// </summary>
        /// <param name="residentId">Ange Id för den som ska raderas</param>
        /// <returns>Returnera den raderade residenten</returns>
        public static Resident DeleteResident(int residentId)
        {
            Resident resident;
            using (var db = new WasherContext())
            {
                resident = db.Residents
                    //.Include(r => r.Reservations)
                    //.Include(x => x.Cue)
                    .FirstOrDefault(x => x.ResidentId == residentId);

                //.Find(residentId)
                //;
                if (resident != null)
                {
                    db.Residents.Remove(resident);
                }

                try
                {
                    db.SaveChanges();
                }
                catch (Exception)
                {
                    throw;
                }
            }

            return resident;
        }