Exemplo n.º 1
0
        public EventResponse UpdateEvent(PutRequest <Event> putRequest)
        {
            using (var db = new EventSourceDbContext(_dbContext))
            {
                var @event = db.Events.FirstOrDefault(e => e.Id == putRequest.Id);

                if (@event == null)
                {
                    return new EventResponse()
                           {
                               Result = false
                           }
                }
                ;

                @event.Name        = (!string.IsNullOrEmpty(putRequest.Payload.Name)) ? putRequest.Payload.Name : @event.Name;
                @event.Description = (!string.IsNullOrEmpty(putRequest.Payload.Description)) ? putRequest.Payload.Description : @event.Description;
                @event.City        = (!string.IsNullOrEmpty(putRequest.Payload.City)) ? putRequest.Payload.City : @event.City;
                @event.Category    = (!string.IsNullOrEmpty(putRequest.Payload.Category)) ? putRequest.Payload.Category : @event.Category;
                @event.Location    = (!string.IsNullOrEmpty(putRequest.Payload.Location)) ? putRequest.Payload.Location : @event.Location;
                @event.Seats       = (putRequest.Payload.Seats > 0) ? putRequest.Payload.Seats : @event.Seats;

                db.SaveChanges();

                return(new EventResponse()
                {
                    Event = @event
                });
            }
        }
Exemplo n.º 2
0
 public EventResponse GetEvent(EventIdRequest idRequest)
 {
     using (var db = new EventSourceDbContext(_dbContext))
     {
         return(new EventResponse
         {
             Event = db.Events.FirstOrDefault(e => e.Id == idRequest.Id)
         });
     }
 }
Exemplo n.º 3
0
        public Response DeleteEvent(EventIdRequest idRequest)
        {
            using (var db = new EventSourceDbContext(_dbContext))
            {
                var @event = db.Events.Find(idRequest.Id);

                db.Events.Remove(@event);
                db.SaveChanges();

                return(new Response());
            }
        }
Exemplo n.º 4
0
        public EventResponse CreateEvent(PostRequest <Event> postRequest)
        {
            using (var db = new EventSourceDbContext(_dbContext))
            {
                db.Events.Add(postRequest.Payload);
                db.SaveChanges();

                return(new EventResponse()
                {
                    Event = postRequest.Payload
                });
            }
        }
Exemplo n.º 5
0
 public PlaceResponse GetPlace(PlaceIdRequest request)
 {
     using (var db = new EventSourceDbContext(_contextOptions))
     {
         var place = db.Places.Find(request.Id);
         if (place != null)
         {
             return(new PlaceResponse()
             {
                 Place = place
             });
         }
         return(new PlaceResponse()
         {
             Place = place, Result = false
         });
     }
 }
Exemplo n.º 6
0
        public Response DeletePlace(PlaceIdRequest request)
        {
            var response = new Response();

            using (var db = new EventSourceDbContext(_contextOptions))
            {
                var _place = db.Places.Find(request.Id);

                if (_place == null)
                {
                    response.Result = false;
                    return(response);
                }
                db.Places.Remove(_place);
                db.SaveChanges();
                return(response);
            }
        }
Exemplo n.º 7
0
        public EventsResponse GetEvents(EventSearchRequest searchRequest)
        {
            Expression <Func <string, string, bool> > startsWithExpression = (e, r) => e.ToLower().StartsWith(r.ToLower());
            Expression <Func <string, string, bool> > containsExpression   = (e, r) => e.ToLower().Contains(r.ToLower());
            Func <string, string, bool> startsWith = startsWithExpression.Compile();
            Func <string, string, bool> contains   = containsExpression.Compile();

            using (var db = new EventSourceDbContext(_dbContext))
            {
                var events = db.Events.Select(e => e);

                if (!string.IsNullOrEmpty(searchRequest.Name))
                {
                    events = events.Where(e => startsWith(e.Name, searchRequest.Name));
                }

                if (!string.IsNullOrEmpty(searchRequest.City))
                {
                    events = events.Where(e => contains(e.City, searchRequest.City));
                }

                if (!string.IsNullOrEmpty(searchRequest.Category))
                {
                    events = events.Where(e => contains(e.Category, searchRequest.Category));
                }

                if (!string.IsNullOrEmpty(searchRequest.Location))
                {
                    events = events.Where(e => contains(e.Location, searchRequest.Location));
                }

                return(new EventsResponse()
                {
                    TotalCount = events.Count(),
                    Events = events
                             .OrderBy(e => e.Name)
                             .Skip(searchRequest.Offset)
                             .Take(searchRequest.Limit)
                             .ToList()
                });
            }
        }
Exemplo n.º 8
0
 public PlaceResponse CreatePlace(PostRequest <Place> request)
 {
     using (var db = new EventSourceDbContext(_contextOptions))
     {
         var newPlace = new Place()
         {
             DateRegistered = request.Payload.DateRegistered,
             Capacity       = request.Payload.Capacity,
             Location       = request.Payload.Location.ToLower(),
             Name           = request.Payload.Name.ToLower(),
             Description    = request.Payload.Description,
             City           = request.Payload.City.ToLower()
         };
         db.Places.Add(newPlace);
         db.SaveChanges();
         return(new PlaceResponse()
         {
             Place = newPlace,
         });
     }
 }
Exemplo n.º 9
0
        public PlaceResponse UpdatePlace(PutRequest <Place> request)
        {
            using (var db = new EventSourceDbContext(_contextOptions))
            {
                var response = new PlaceResponse();
                var _place   = db.Places.Find(request.Id);

                if (_place != null)
                {
                    _place.Description = !string.IsNullOrEmpty(request.Payload.Description) ? request.Payload.Description : _place.Description;
                    _place.City        = !string.IsNullOrEmpty(request.Payload.City) ? request.Payload.City.ToLower() : _place.City;
                    _place.Capacity    = request.Payload.Capacity > 0 ? request.Payload.Capacity : _place.Capacity;
                    _place.Name        = !string.IsNullOrEmpty(request.Payload.Name) ? request.Payload.Name.ToLower() : _place.Name;
                    _place.Location    = !string.IsNullOrEmpty(request.Payload.Location) ? request.Payload.Location.ToLower() : _place.Location;

                    db.Places.Attach(_place);
                    db.SaveChanges();
                    response.Place = _place;
                    return(response);
                }
                response.Result = false;
                return(response);
            }
        }
Exemplo n.º 10
0
        public PlacesResponse GetAllPlaces(PlaceSearchRequest placeRequest)
        {
            Expression <Func <string, string, bool> > startsWith = (p, e) => p.ToLower().StartsWith(e.ToLower());
            Expression <Func <string, string, bool> > contains   = (p, e) => p.ToLower().Contains(e.ToLower());

            using (var db = new EventSourceDbContext(_contextOptions))
            {
                IQueryable <Place> placeQuery = db.Places;
                placeQuery = !string.IsNullOrEmpty(placeRequest.Name) ? placeQuery.Where(p => startsWith.Compile()(p.Name, placeRequest.Name)) :
                             !string.IsNullOrEmpty(placeRequest.City) ? placeQuery.Where(p => contains.Compile()(p.City, placeRequest.City)) :
                             !string.IsNullOrEmpty(placeRequest.Location) ? placeQuery.Where(p => contains.Compile()(p.Location, placeRequest.Location)) : placeQuery;


                var places = placeQuery
                             .OrderBy(p => p.Name)
                             .Skip(placeRequest.Offset)
                             .Take(placeRequest.Limit)
                             .ToList();

                return(new PlacesResponse {
                    Places = places, TotalPlaces = places.Count()
                });
            }
        }