Ejemplo n.º 1
0
        public async Task <IActionResult> ReplaceObject([FromRoute] ItemRouteParameters routeParameters, [FromBody] string json, [FromQuery] ResponseFormat responseFormat = ResponseFormat.EntireObject)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            string document = await _service.ReplaceAsync(routeParameters.DatabaseName, routeParameters.CollectionName, routeParameters.Id, json);

            if (string.IsNullOrEmpty(document))
            {
                return(ObjectNotFound(routeParameters.Id, routeParameters.CollectionName));
            }

            if (responseFormat == ResponseFormat.OnlyId)
            {
                string id = GetObjectId(document);
                document = GetReplacedJsonResult(new string[] { id }).ToString();
                return(Ok(document));
            }
            else
            {
                return(Ok(document));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> GetObject([FromRoute] ItemRouteParameters routeParameters)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var document = await _service.GetAsync(routeParameters.DatabaseName, routeParameters.CollectionName, routeParameters.Id);

            if (document == null)
            {
                return(ObjectNotFound(routeParameters.Id, routeParameters.CollectionName));
            }
            return(Ok(document));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> InsertObjectWithId([FromRoute] ItemRouteParameters routeParameters, [FromBody] string json, [FromQuery] ResponseFormat responseFormat)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            string document = await _service.InsertAsync(routeParameters.DatabaseName, routeParameters.CollectionName, routeParameters.Id, json);

            if (responseFormat == ResponseFormat.OnlyId)
            {
                document = GetInsertedJsonResult(new string[] { routeParameters.Id }).ToString();
            }
            return(CreatedAtAction(nameof(GetObject), new { id = routeParameters.Id, db = routeParameters.DatabaseName, collection = routeParameters.CollectionName }, document));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> DeleteObject([FromRoute] ItemRouteParameters routeParameters)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            bool deleted = await _service.DeleteAsync(routeParameters.DatabaseName, routeParameters.CollectionName, routeParameters.Id);

            if (deleted)
            {
                return(Ok());
            }
            else
            {
                return(ObjectNotFound(routeParameters.Id, routeParameters.CollectionName));
            }
        }