Exemplo n.º 1
0
        public async Task <DataResponse <CarpoolRequest> > CreateCarpoolRequest(int userId, DateTime fromTime, DateTime toTime, string zipCode, string city, List <Event> events)
        {
            using (var context = CreateContext())
            {
                try
                {
                    // Create carpool request
                    CarpoolRequest carpoolRequest = new CarpoolRequest()
                    {
                        PassengerId = userId,
                        FromTime    = fromTime,
                        ToTime      = toTime,
                        ZipCode     = zipCode,
                        City        = city,
                        StateId     = 1
                    };
                    // Add new carpool request
                    var entry = await context.CarpoolRequests.AddAsync(carpoolRequest);

                    // Save changes
                    int rowsChanged = await context.SaveChangesAsync();

                    // Error when saving
                    if (rowsChanged != 1)
                    {
                        return(new DataResponse <CarpoolRequest>(300, Resources.AppResources.could_not_create_carpool_request));
                    }
                    // Create event relations
                    var event_relations = new List <CarpoolRequests_Events_Relations>();
                    foreach (var eve in events)
                    {
                        event_relations.Add(new CarpoolRequests_Events_Relations()
                        {
                            CarpoolRequestId = entry.Entity.Id,
                            EventId          = eve.Id
                        });
                    }
                    // Add new relations
                    await context.CarpoolRequests_Events_Relations.AddRangeAsync(event_relations);

                    // Save changes
                    rowsChanged = await context.SaveChangesAsync();

                    // Error when saving
                    if (rowsChanged != event_relations.Count)
                    {
                        return(new DataResponse <CarpoolRequest>(301, Resources.AppResources.could_not_add_events_to_carpool_request));
                    }
                    // Return response
                    return(new DataResponse <CarpoolRequest>(true, entry.Entity));
                }
                catch (Exception mes)
                {
                    // Return exception
                    return(new DataResponse <CarpoolRequest>(1, mes.Message));
                }
            }
        }
Exemplo n.º 2
0
        public async Task <DataResponse> InvitePassenger(int carpoolRequestId, int carpoolEventId)
        {
            using (var context = CreateContext())
            {
                try
                {
                    // Get carpool request
                    CarpoolRequest carpoolRequest = await context.CarpoolRequests.FirstOrDefaultAsync(x => x.Id == carpoolRequestId);

                    // Set carpool request to inactive
                    carpoolRequest.StateId = 2;
                    // Create carpool confirmation
                    CarpoolConfirmation carpoolConfirmation = new CarpoolConfirmation()
                    {
                        CarpoolEventId       = carpoolEventId,
                        HasDriverAccepted    = true,
                        HasPassengerAccepted = false,
                        StateId     = 1,
                        PassengerId = carpoolRequest.PassengerId
                    };
                    // Add new carpool confirmation
                    await context.CarpoolConfirmations.AddAsync(carpoolConfirmation);

                    // Save changes
                    int rowsChanged = await context.SaveChangesAsync();

                    // Error when saving
                    if (rowsChanged < 1)
                    {
                        return(new DataResponse(401, Resources.AppResources.could_not_invite_passenger));
                    }
                    // Return response
                    return(new DataResponse(true));
                }
                catch (Exception mes)
                {
                    // Return exception
                    return(new DataResponse(1, mes.Message));
                }
            }
        }