public IActionResult Post([FromBody] XXXX value)
 {
     if (value == null)
     {
         return(BadRequest());
     }
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     if (!_repository.Add(value).Result)
     {
         return(StatusCode(500, "A problem while handling your request!"));
     }
     return(CreatedAtAction("Get", new { id = value.ID }, value));
 }
Exemple #2
0
        public async Task <bool> Update(int id, XXXX item)
        {
            try
            {
                item.ID = id; //Make sure ID of the item is assigned.
                ReplaceOneResult actionResult = await _context.XXXXs
                                                .ReplaceOneAsync(n => n.ID.Equals(id)
                                                                 , item
                                                                 , new UpdateOptions { IsUpsert = true });

                return(actionResult.IsAcknowledged &&
                       actionResult.ModifiedCount > 0);
            }
            catch (Exception ex)
            {
                _logger.Warning("Something went wrong while updating XXXX with ID {@ID}.", id);
                throw ex;
            }
        }
 public IActionResult Put(int id, [FromBody] XXXX value)
 {
     if (value == null || id < 0)
     {
         return(BadRequest());
     }
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     if (!_repository.Exists(id).Result)
     {
         return(NotFound());
     }
     value.ID = id;
     if (_repository.Update(id, value).Result)
     {
         return(StatusCode(202, "Updated Successfully!"));
     }
     return(NoContent());
 }
Exemple #4
0
        public async Task <bool> Add(XXXX item)
        {
            try
            {
                var filter = Builders <XXXX> .Filter.Eq(x => x.ID, item.ID);

                var o = _context.XXXXs
                        .Find(filter)
                        .FirstOrDefaultAsync()
                        .Result;
                if (o == null)
                {
                    await _context.XXXXs.InsertOneAsync(item);

                    return(true);
                }
                return(false);
            }
            catch (Exception ex)
            {
                _logger.Warning("Something went wrong while adding XXXX with ID {@ID}.", item.ID);
                throw ex;
            }
        }