Пример #1
0
        public virtual async Task AddItinerary(AmblOnGraph amblGraph, string username, string entLookup, Itinerary itinerary)
        {
            ensureStateObject();

            itinerary.CreatedDateTime = DateTime.Now;
            itinerary.Shared          = false;
            itinerary.Editable        = true;

            var itineraryResp = await amblGraph.AddItinerary(username, entLookup, itinerary);

            if (itineraryResp.Status)
            {
                itinerary.ID = itineraryResp.Model;

                await itinerary.ActivityGroups.Each(async (activityGroup) =>
                {
                    activityGroup.CreatedDateTime = DateTime.Now;

                    var activityGroupResp = await amblGraph.AddActivityGroup(username, entLookup, itinerary.ID, activityGroup);

                    if (activityGroupResp.Status)
                    {
                        activityGroup.ID = activityGroupResp.Model;

                        await activityGroup.Activities.Each(async(activity) =>
                        {
                            activity.CreatedDateTime = DateTime.Now;

                            var activityResp = await amblGraph.AddActivityToAG(username, entLookup, itinerary.ID, activityGroup.ID, activity);

                            if (activityResp.Status)
                            {
                                activity.ID = activityResp.Model;
                            }
                        });
                    }
                });

                if (!State.UserItineraries.Any(x => x.ID == itinerary.ID))
                {
                    State.UserItineraries.Add(itinerary);
                }
            }

            State.Loading = false;
        }
Пример #2
0
        public virtual async Task EditItinerary(AmblOnGraph amblGraph, string username, string entLookup, Itinerary itinerary, List <ActivityLocationLookup> activityLocations)
        {
            State.Loading = true;

            ensureStateObject();

            var activitiesList = new List <Activity>();

            if (!activityLocations.IsNullOrEmpty())
            {
                activitiesList = await addLocationFromActivity(amblGraph, username, entLookup, activityLocations);
            }

            var existing = State.UserItineraries.FirstOrDefault(x => x.ID == itinerary.ID);

            if (existing != null)
            {
                if (existing.Editable)
                {
                    var success = true;

                    var itineraryResp = await amblGraph.EditItinerary(username, entLookup, itinerary);

                    if (!itineraryResp.Status)
                    {
                        success = false;
                    }

                    if (success)
                    {
                        await itinerary.ActivityGroups.Each(async (activityGroup) =>
                        {
                            var agExisting = existing.ActivityGroups.FirstOrDefault(x => x.ID == activityGroup.ID);

                            if (agExisting == null)
                            {
                                var addActGResp = await amblGraph.AddActivityGroup(username, entLookup, itinerary.ID, activityGroup);

                                if (addActGResp.Status)
                                {
                                    activityGroup.ID = addActGResp.Model;

                                    await activityGroup.Activities.Each(async(activity) =>
                                    {
                                        var addActResp = new BaseResponse <Guid>();

                                        if (activity.ID == Guid.Empty)
                                        {
                                            addActResp = await amblGraph.AddActivityToAG(username, entLookup, itinerary.ID, activityGroup.ID, activity);

                                            activity.ID = addActResp.Model;

                                            var exists = activitiesList?.FirstOrDefault(x => x.Title == activity.Title);

                                            if (exists != null)
                                            {
                                                exists.ID = activity.ID;

                                                addActResp = await amblGraph.AddActivityToAG(username, entLookup, itinerary.ID, activityGroup.ID, exists);
                                            }
                                        }

                                        else
                                        {
                                            var exists = activitiesList?.FirstOrDefault(x => x.ID == activity.ID);

                                            addActResp = await amblGraph.AddActivityToAG(username, entLookup, itinerary.ID, activityGroup.ID, exists);
                                        }

                                        activity.ID = addActResp.Model;

                                        if (!addActResp.Status)
                                        {
                                            success = false;
                                        }
                                    });
                                }
                                else
                                {
                                    success = false;
                                }
                            }
                            else
                            {
                                await activityGroup.Activities.Each(async(activity) =>
                                {
                                    var aExisting = agExisting.Activities.FirstOrDefault(x => x.ID == activity.ID);

                                    if (aExisting == null)
                                    {
                                        var exists = activitiesList?.FirstOrDefault(x => x.ID == activity.ID);

                                        var addActResp = await amblGraph.AddActivityToAG(username, entLookup, itinerary.ID, activityGroup.ID, exists ?? activity);

                                        activitiesList.Remove(exists);

                                        activity.ID = addActResp.Model;

                                        if (!addActResp.Status)
                                        {
                                            success = false;
                                        }
                                    }
                                    else
                                    {
                                        var exists = activitiesList?.FirstOrDefault(x => x.ID == activity.ID);

                                        var editActResp = await amblGraph.EditActivity(username, entLookup, exists ?? activity);

                                        if (!editActResp.Status)
                                        {
                                            success = false;
                                        }
                                    }
                                });

                                var editActGResp = await amblGraph.EditActivityGroup(username, entLookup, activityGroup);

                                if (!editActGResp.Status)
                                {
                                    success = false;
                                }
                            }
                        });

                        await existing.ActivityGroups.Each(async (activityGroup) =>
                        {
                            var agNew = itinerary.ActivityGroups.FirstOrDefault(x => x.ID == activityGroup.ID);

                            if (agNew == null)
                            {
                                await activityGroup.Activities.Each(async(activity) =>
                                {
                                    var delActResp = await amblGraph.DeleteActivity(username, entLookup, itinerary.ID, activityGroup.ID, activity.ID);

                                    if (!delActResp.Status)
                                    {
                                        success = false;
                                    }
                                });

                                if (success)
                                {
                                    var delActGResp = await amblGraph.DeleteActivityGroup(username, entLookup, itinerary.ID, activityGroup.ID);

                                    if (!delActGResp.Status)
                                    {
                                        success = false;
                                    }
                                }
                            }
                            else
                            {
                                await activityGroup.Activities.Each(async(activity) =>
                                {
                                    var aNew = agNew.Activities.FirstOrDefault(x => x.ID == activity.ID);

                                    if (aNew == null)
                                    {
                                        var delActResp = await amblGraph.DeleteActivity(username, entLookup, itinerary.ID, activityGroup.ID, activity.ID);

                                        if (!delActResp.Status)
                                        {
                                            success = false;
                                        }
                                    }
                                });
                            }
                        });
                    }

                    if (success)
                    {
                        State.UserItineraries = await fetchUserItineraries(amblGraph, username, entLookup);
                    }
                    else
                    {
                        State.Error = "General Error updating user itinerary.";
                    }
                }
                else
                {
                    State.Error = "Cannot edit a shared itinerary.";
                }
            }
            else
            {
                State.Error = "Itinerary not found.";
            }

            State.Loading = false;
        }