Пример #1
0
        public EventModel Create(CreateEventRequest eventModel, string ownerToken)
        {
            if (_events.Find(e => e.Title == eventModel.Title).FirstOrDefault() == null)
            {
                var owner        = _users.Find(u => u.Token == ownerToken).FirstOrDefault();
                var createdEvent = new BackendEventModel(null, eventModel.Title, owner.City, eventModel.DetailLocation
                                                         , eventModel.Description, owner.Id, eventModel.Date, eventModel.Price);
                _events.InsertOne(createdEvent);
                _redisEventsDatabase.StringSet(createdEvent.Id, JsonConvert.SerializeObject(createdEvent), ttl);
                createdEvent = _convertModelsService.GetBackendEventModel(GetByTitle(eventModel.Title));

                if (eventModel.ImageArray != null)
                {
                    var imageUrl = UploadImage(createdEvent.Id, eventModel.ImageArray);
                    createdEvent.ImageUrl = imageUrl;
                    _events.ReplaceOne(e => e.Id == createdEvent.Id, createdEvent);
                    _redisEventsDatabase.StringSet(createdEvent.Id, JsonConvert.SerializeObject(createdEvent), ttl);
                }

                var list = new List <string>();
                if (owner.HostingEvents != null)
                {
                    list = owner.HostingEvents.ToList();
                }
                list.Add(createdEvent.Id);
                owner.HostingEvents = list.ToArray();
                _users.ReplaceOne(u => u.Id == owner.Id, owner);
                _redisUsersDatabase.StringSet(owner.Id, JsonConvert.SerializeObject(owner), ttl);

                return(_convertModelsService.GetEventModel(createdEvent));
            }


            return(null);
        }
        public EventModel GetEventModel(BackendEventModel backendEventModel)
        {
            var eventModel = EventModel.ConvertToEventModel(backendEventModel);

            var owner = JsonConvert.DeserializeObject <BackendUserModel>(
                _redisUsersDatabase.StringGet(backendEventModel.Owner));
            var visitors = new List <UserModel>();

            if (backendEventModel.Visitors != null)
            {
                foreach (var id in backendEventModel.Visitors)
                {
                    visitors.Add(GetUserModel(JsonConvert.DeserializeObject <BackendUserModel>(
                                                  _redisUsersDatabase.StringGet(id))));
                }
            }

            eventModel.Owner    = GetUserModel(owner);
            eventModel.Visitors = visitors.ToArray();

            return(eventModel);
        }
        public BackendEventModel GetBackendEventModel(EventModel eventModel)
        {
            var backendEventModel = BackendEventModel.ConvertToBackendEventModel(eventModel);

            var owner = JsonConvert.DeserializeObject <BackendEventModel>(
                _redisUsersDatabase.StringGet(eventModel.Owner.Id));

            var visitors = new List <string>();

            if (eventModel.Visitors != null)
            {
                foreach (var e in eventModel.Visitors)
                {
                    visitors.Add(e.Id);
                }
            }

            backendEventModel.Owner    = owner.Id;
            backendEventModel.Visitors = visitors.ToArray();

            return(backendEventModel);
        }