コード例 #1
0
        public async Task <CourseDetailViewModel> EditCourseAsync(CourseEditInputModel inputModel)
        {
            //verifica quante righe esistono con quell'id
            //se ritorna 0: non c'é alcun corso corrispondente al criterio

            /*bool courseExists = await db.ExecuteQueryScalarAsync<bool>($"SELECT COUNT(*) FROM Courses WHERE Id={inputModel.Id}");
             * if (!courseExists)
             * {
             *  throw new CourseNotFoundException(inputModel.Id);   //e sollevo un eccezione impedendo lo svolgimento delle funzioni a seguire
             * }*/

            try
            {
                string imagePath = null;
                if (inputModel.Image != null)
                {
                    imagePath = await imagePersister.SaveCourseImageAsync(inputModel.Id, inputModel.Image);
                }
                //COALESCE: può accettare un qualsiasi numero di argomenti e restituisce il primo che trova NON NULL
                //se image path é null (nessuna img fornita), restituisce il valore stesso del campo imagePath (riassegnato su se stesso)

                int affectedRows = await db.CommandAsync($"UPDATE Courses SET ImagePath=COALESCE({imagePath}, ImagePath), Title={inputModel.Title}, Description={inputModel.Description}, Email={inputModel.Email}, CurrentPrice_Currency={inputModel.CurrentPrice.Currency.ToString()}, CurrentPrice_Amount={inputModel.CurrentPrice.Amount}, FullPrice_Currency={inputModel.FullPrice.Currency.ToString()}, FullPrice_Amount={inputModel.FullPrice.Amount} WHERE Id={inputModel.Id} AND RowVersion={inputModel.RowVersion}");

                if (affectedRows == 0)  //se il numero di righe interessate é 0
                {
                    //verifichiamo se il corso esiste
                    bool courseExists = await db.ExecuteQueryScalarAsync <bool>($"SELECT COUNT(*) FROM Courses WHERE Id={inputModel.Id}"); //count = 1: corso esistente, count = 0: corso non esiste

                    if (courseExists)                                                                                                      //se il corso esiste
                    {
                        //allora é fallito il controllo sulla rowVersion (concorrenza ottimistica)
                        throw new OptimisticConcurrencyException();
                    }
                    else //se il corso non dovesse esistere
                    {
                        throw new CourseNotFoundException(inputModel.Id);
                    }
                }
            }
            catch (ConstraintViolationException ex)
            {
                //eccezione personalizzata: creazione del corso fallita perché il titolo é già in utilizzo da un altro corso
                throw new CourseTitleUnavailableException(inputModel.Title, ex);
            }
            catch (ImagePersistenceException ex) //immagine troppo grande!
            {
                throw new CourseImageInvalidException(inputModel.Id, ex);
            }

            /*if (inputModel.Image != null)
             * {
             *  try{
             *      string imagePath = await imagePersister.SaveCourseImageAsync(inputModel.Id, inputModel.Image);
             *      //aggiorno solo l'image path con il nuovo percorso che é stato restituito
             *      dataSet = await db.ExecuteQueryAsync($"UPDATE Courses SET ImagePath={imagePath} WHERE Id={inputModel.Id}");
             *  }
             *  catch(Exception ex) //immagine troppo grande!
             *  {
             *      throw new CourseImageInvalidException(inputModel.Id, ex);
             *  }
             *
             * }*/

            CourseDetailViewModel course = await GetCourseAsync(inputModel.Id);

            return(course);
        }