Exemple #1
0
 public async Task <ActionResult <SamuraiModel> > Post(SamuraiModel model)
 {
     try
     {
         var location = linkGenerator.GetPathByAction("Get", "Samurai", new { model.Id });
         var samurai  = mapper.Map <Samurai>(model);
         samuraiData.Add(samurai);
         if (await samuraiData.SaveChangesAsync())
         {
             return(Created(location, mapper.Map <SamuraiModel>(samurai)));
         }
     }
     catch (Exception)
     {
         return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failiure"));
     }
     return(BadRequest());
 }
Exemple #2
0
        public async Task <ActionResult <SamuraiModel> > Update(int id, SamuraiModel model)
        {
            try
            {
                var oldSamurai = await samuraiData.GetSamuraiByIdAsync(id);

                if (oldSamurai == null)
                {
                    return(NotFound("Samurai is not found"));
                }
                mapper.Map(model, oldSamurai);
                if (await samuraiData.SaveChangesAsync())
                {
                    return(mapper.Map <SamuraiModel>(oldSamurai));
                }
            }
            catch (Exception)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failiure"));
            }
            return(BadRequest());
        }