Ejemplo n.º 1
0
        public async Task <IActionResult> UpdateCategoryAsync(CalendarEventCategory category,
                                                              [FromServices] IMongoCollection <CalendarEventCategory> mongoCollection)
        {
            if (category == null || !category.AreValuesCorrect())
            {
                return(this.Error(HttpStatusCode.UnprocessableEntity,
                                  "CalendarEventCategory is null or it's properties are empty!"));
            }
            CalendarEventCategory currentValue = await mongoCollection.FirstOrDefaultAsync(x => x.ID == category.ID);

            if (currentValue == null)
            {
                return(this.Error(HttpStatusCode.UnprocessableEntity,
                                  "Sended CalendarEventCategory to update has altered Id! Unable to update value!"));
            }
            UpdateResult result = await mongoCollection.UpdateOneAsync(x => x.ID == category.ID,
                                                                       Extensions.GenerateUpdateDefinition <CalendarEventCategory>(currentValue, category));

            if (result.IsAcknowledged)
            {
                return(this.Success(""));
            }
            else
            {
                return(this.Error(HttpStatusCode.InternalServerError, "Value wasn't updated!"));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> AddCategoryAsync(CalendarEventCategory category,
                                                           [FromServices] IMongoCollection <CalendarEventCategory> mongoCollection)
        {
            if (category == null || !category.AreValuesCorrect())
            {
                return(this.Error(HttpStatusCode.UnprocessableEntity,
                                  "CalendarEventCategory is null or it's properties are empty!"));
            }
            category.ID = Guid.NewGuid();
            await mongoCollection.InsertOneAsync(category);

            return(this.Success(category.ID));
        }