Ejemplo n.º 1
0
        public TipResponse GetTip(string tipId)
        {
            // --- Obtener Tip Guid e intentar buscarlo ---
            UserTipHistory tip
                = Database.UserTipHistoryStore.Get(tipId);

            if (tip == null)
            {
                throw new HttpNotFoundException(
                          ControllerStrings.Warning801_TipNotFound
                          );
            }

            // --- Obtener Categoría de Tip ---
            TipCategory tipCategory
                = tip.Tip.TipCategory;

            // --- Preparar y enviar la respuesta ---
            return(new TipResponse()
            {
                TipId
                    = tip.Guid.ToBase64String(),
                TipCategory
                    = new TipCategoryResponse {
                        TipCategoryId
                            = tipCategory.Guid.ToBase64String(),
                        Name
                            = tipCategory.GetGlobalization().Name,
                        Description
                            = tipCategory.GetGlobalization().Description
                    },

                Timestamp
                    = tip.CreationDate,
                Text
                    = tip.Tip.GetGlobalization().Text,
                Source
                    = tip.Tip.GetGlobalization().Source
            });
        }
Ejemplo n.º 2
0
        public IEnumerable <TipResponse> GetTipsHistory(int page = 1, int perPage = 20)
        {
            // --- Verificar si se tiene la cabecera {If-Modified-Since} ---
            DateTimeOffset?ifModifiedSince
                = Request.Headers.IfModifiedSince;

            if (ifModifiedSince.HasValue)
            {
                UserTipHistory lastTipHistory
                    = Database.UserTipHistoryStore.GetFirst(
                          filter: f =>
                          f.User.Guid == CurrentUser.Guid,
                          orderBy: o =>
                          o.OrderBy(b => b.CreationDate)
                          );

                if (lastTipHistory != null && ifModifiedSince.Value.UtcDateTime > lastTipHistory.CreationDate)
                {
                    throw new HttpNotModifiedException();
                }
            }

            // --- Obtener los Tips conseguidos ---
            IEnumerable <UserTipHistory> tipsHistory
                = Database.UserTipHistoryStore.GetAll(
                      filter: f =>
                      f.User.Guid == CurrentUser.Guid,
                      orderBy: o =>
                      o.OrderByDescending(b => b.CreationDate),
                      extra: x =>
                      x.Skip((page - 1) * perPage).Take(perPage),
                      include:
                      new string[] { "Tip.TipCategory" }
                      );

            // --- Preparar respuesta ---
            List <TipResponse> response
                = new List <TipResponse>();
            Dictionary <Guid, TipCategoryResponse> tipCategoryLocales
                = new Dictionary <Guid, TipCategoryResponse>();

            foreach (UserTipHistory tipHistory in tipsHistory)
            {
                // Obtener Tip en el Idioma actual
                TipGlobalization tipGlobalization
                    = tipHistory.Tip.GetGlobalization();

                // Añadir el Tip sólo si se tienen textos en idioma solicitado
                if (string.IsNullOrEmpty(tipGlobalization.Text))
                {
                    continue;
                }

                // Obtener Categoría de Tip
                TipCategory tipCategory
                    = tipHistory.Tip.TipCategory;

                TipCategoryResponse tipCategoryResponse;

                if (!tipCategoryLocales.ContainsKey(tipCategory.Guid))
                {
                    tipCategoryLocales.Add(
                        tipCategory.Guid,
                        new TipCategoryResponse {
                        TipCategoryId
                            = tipCategory.Guid.ToBase64String(),
                        Name
                            = tipCategory.GetGlobalization().Name,
                        Description
                            = tipCategory.GetGlobalization().Description,
                    }
                        );
                }

                tipCategoryResponse
                    = tipCategoryLocales[tipCategory.Guid];

                response.Add(new TipResponse()
                {
                    TipId
                        = tipHistory.Guid.ToBase64String(),
                    TipCategory
                        = tipCategoryResponse,

                    Timestamp
                        = tipHistory.CreationDate,
                    Text
                        = tipGlobalization.Text,
                    Source
                        = tipGlobalization.Source
                });
            }

            return(response);
        }