public AppointmentViewModel(Appointment appointment)
 {
     this.StartTime = appointment.StartTime;
     this.EndTime = appointment.EndTime;
     this.Price = appointment.Price;
     this.Location = appointment.Location;
     this.CustomerName = appointment.Customer.UserName;
     this.Id = appointment.Id;
 }
 public CustomerAppointmentViewModel(Appointment appointment)
 {
     this.Id = appointment.Id;
     this.StartTime = appointment.StartTime;
     this.EndTime = appointment.EndTime;
     this.Price = appointment.Price;
     this.Location = appointment.Location;
     this.EscortName = appointment.Escort.UserName;
     this.IsCanceled = appointment.IsCanceled;
     if (appointment.IsApproved != null)
     {
         this.IsApproved = (bool) appointment.IsApproved;
     }
     this.IsExpired = DateTime.Now > appointment.EndTime;
 }
        public IHttpActionResult SubmitAppointment(SubmitAppointmentBindingModel appData)
        {
            if (appData == null)
            {
                return this.BadRequest("Missing appointment data");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var currentUserId = this.User.Identity.GetUserId();

            if (!this.EscortServiceData.Customers.Any(c => c.Id == currentUserId))
            {
                return this.Unauthorized();
            }

            var escortId =
                this.EscortServiceData.Escorts.Where(c => !c.IsDeleted && c.UserName == appData.EscortName).Select(c => c.Id).FirstOrDefault();

            if (escortId == null)
            {
                return this.Content(HttpStatusCode.BadRequest,
                    string.Format("There is no existing active escort with user name: {0}", appData.EscortName));
            }

            //We don't need to list all escort's appointments here. We can just check whether there is an appointment in this time period.
            var appointmentsForEscort = this.EscortServiceData.Appointments.Where(a => a.EscortId == escortId).ToList();
            var appStartTime = DateTime.Parse(appData.StartTime);
            var appEndTime = DateTime.Parse(appData.EndTime);

            foreach (var appointment in appointmentsForEscort)
            {
                if ((appStartTime >= appointment.StartTime && appStartTime <= appointment.EndTime) || (appEndTime >= appointment.StartTime && appEndTime <= appointment.EndTime))
                {
                    return
                        this.BadRequest(String.Format("Escort {0} is occupiet in period: {1}  to  {2}",
                            appData.EscortName, appointment.StartTime, appointment.EndTime));
                }
            }

            Appointment newAppointment = new Appointment()
            {
                StartTime = DateTime.Parse(appData.StartTime),
                EndTime = DateTime.Parse(appData.EndTime),
                Price = appData.Price,
                Location = appData.Location,
                IsApproved = false,
                IsCanceled = false,
                CustomerId = currentUserId,
                EscortId = escortId,
            };

            this.EscortServiceData.Appointments.Add(newAppointment);
            this.EscortServiceData.SaveChanges();

            return
                this.Ok(String.Format(
                    "Appointment from customer with id: {0} to escort with username: {1} was submited", currentUserId,
                    appData.EscortName));
        }