예제 #1
0
        public async Task <IActionResult> Get(string group, string name, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(group))
            {
                return(new ErrorMessageResult("No category group specified.", HttpStatusCode.NotFound));
            }

            if (Enum.TryParse <CategoryGroup>(group, true, out var categoryGroup) == false)
            {
                return(new ErrorMessageResult("Category group specified is invalid.", HttpStatusCode.NotFound));
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                return(new ErrorMessageResult("No category name specified.", HttpStatusCode.NotFound));
            }

            var readType = ReadType.VisibleOnly;

            // Check if there is an authenticated user who is an administrator
            // Administrators will receive the full category with all the properties
            // Non administrators (both anonymous users and authenticated users) will receive
            // only a visible category with only the public properties being returned
            var isAdministrator = IsAdministrator();

            if (isAdministrator)
            {
                readType = ReadType.All;
            }

            var category = await _query.GetCategory(readType, categoryGroup, name, cancellationToken)
                           .ConfigureAwait(false);

            if (category == null)
            {
                return(new ErrorMessageResult("The requested category does not exist.", HttpStatusCode.NotFound));
            }

            if (isAdministrator)
            {
                return(new OkObjectResult(category));
            }

            var publicCategory = new PublicCategory(category);

            return(new OkObjectResult(publicCategory));
        }
예제 #2
0
        public async Task <IHttpActionResult> Get(long id)
        {
            var category = await _categoryQuery.GetCategory(id);

            return(Ok(category));
        }