/// <summary>
        ///
        /// </summary>
        /// <param name="feedback"></param>
        /// <returns></returns>
        public async Task <Feedback> SaveFeedbackAsync(Feedback feedback)
        {
            if (feedback == null)
            {
                throw new FriendlyException(FriendlyExceptionType.InvalidModelState);
            }
            Logger.WriteDebug("Feedback:" + JsonConvert.SerializeObject(feedback));
            if (!LoggedIn)
            {
                var restroom = await RestroomUnitOfWork.GetRestroomAsync(feedback.RestroomId);

                if (!restroom.IsPublic)
                {
                    throw new FriendlyException(FriendlyExceptionType.AccessDenied);
                }
            }

            if (LoggedIn && feedback.Badge != ClaimsPrincipal.Current.Identity.Name)
            {
                throw new UnauthorizedAccessException("You can't post a feedback for someone else.");
            }
            if (feedback.Rating < 3 && string.IsNullOrWhiteSpace(feedback.FeedbackText))
            {
                throw new FriendlyException(FriendlyExceptionType.FeedbackTextRequired);
            }
            var result = await RestroomUnitOfWork.SaveFeedbackAsync(feedback.ToDataAccess());

            return(Feedback.FromDataAccess(result));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="restroomId"></param>
        /// <param name="startPage"></param>
        /// <param name="numRecords"></param>
        /// <param name="earliestDate"></param>
        /// <returns></returns>
        public async Task <Feedback[]> GetFeedbacksAsync(int restroomId, int?startPage = null, int?numRecords = null, DateTime?earliestDate = null)
        {
            if (!LoggedIn)
            {
                var restroom = await RestroomUnitOfWork.GetRestroomAsync(restroomId);

                if (!restroom.IsPublic)
                {
                    throw new FriendlyException(FriendlyExceptionType.AccessDenied);
                }
            }

            var feedbacks = RestroomUnitOfWork.Get <entity.Feedback>().Where(f => f.RestroomId == restroomId);

            if (earliestDate.HasValue)
            {
                feedbacks = feedbacks.Where(f => f.AddDateTime >= earliestDate.Value);
            }

            if (numRecords.HasValue)
            {
                feedbacks = feedbacks
                            .OrderByDescending(f => f.AddDateTime)
                            .Skip(startPage.GetValueOrDefault() * numRecords.Value)
                            .Take(numRecords.Value);
            }

            var result = await feedbacks.ToListAsync();

            return(result.Select(Feedback.FromDataAccess).ToArray());
        }
        internal async Task <Restroom> GetRestroomAsync(int restroomId)
        {
            Entity.Contact contact   = null;
            var            restroomD = await RestroomUnitOfWork.GetRestroomAsync(restroomId);

            if (restroomD != null && restroomD.ContactId.HasValue)
            {
                contact = RestroomUnitOfWork.GetById <Entity.Contact, int>(restroomD.ContactId.Value);
            }

            return(restroomD == null ? null : Restroom.FromDataAccess(restroomD, contact));
        }
        internal async Task <Restroom> SaveRestroomAsync(Restroom model)
        {
            Entity.Restroom restroom = null;
            if (model.RestroomId > 0)
            {
                var r = await RestroomUnitOfWork.GetRestroomAsync(model.RestroomId);

                restroom = model.ToDataAccessFrom(r);
                Logger.WriteDebug("SaveRestroomAsync->After GetRestroomAsync and ToDataAccessFrom:" + JsonConvert.SerializeObject(restroom));
            }
            else
            {
                restroom           = model.ToDataAccess();
                restroom.UpdUserId = CurrentUserName;
            }

            var hasContact     = false;
            var contactChanged = false;


            hasContact = !string.IsNullOrWhiteSpace(model.ContactName) ||
                         !string.IsNullOrWhiteSpace(model.ContactTitle) ||
                         !string.IsNullOrWhiteSpace(model.ContactEmail) ||
                         !string.IsNullOrWhiteSpace(model.ContactPhone);
            Entity.Contact contact = null;

            if (!hasContact && restroom.ContactId.HasValue && restroom.ContactId > 0)
            {
                restroom.ContactId = null;
                restroom.Contact   = null;
            }

            else if (restroom.ContactId.HasValue && restroom.ContactId > 0)
            {
                contact = await RestroomUnitOfWork.GetByIdAsync <Entity.Contact, int>(restroom.ContactId.Value);

                if (contact.ContactName != model.ContactName ||
                    contact.Title != model.ContactTitle ||
                    contact.Email != model.ContactEmail ||
                    contact.Phone != model.ContactPhone ||
                    contact.ServiceProvider != model.ServiceProvider
                    )
                {
                    contactChanged = true;
                }
            }

            if (contactChanged || (hasContact && (!restroom.ContactId.HasValue || restroom.ContactId == 0)))
            {
                contact = new Entity.Contact
                {
                    Title           = model.ContactTitle,
                    Email           = model.ContactEmail,
                    Phone           = model.ContactPhone,
                    ContactName     = model.ContactName,
                    ServiceProvider = model.ServiceProvider
                };
                contact          = RestroomUnitOfWork.Create(contact);
                restroom.Contact = contact;
            }


            //restroom.Active = true;
            restroom.StatusListId = 1;  // always pending...
            try
            {
                var savedModel = await RestroomUnitOfWork.SaveRestroomAsync(restroom);

                return(savedModel == null ? null : Restroom.FromDataAccess(savedModel));
            }
            catch (DbEntityValidationException ex)
            {
                var errTxt = ex.GetStringRepresentation();
                Logger.WriteError("RestroomHandler.SaveRestroomAsync.DbEntityValidationException -> EntityValidationErrors : \r\n" + errTxt);
                throw;
            }
        }