コード例 #1
0
        public TemaResponse Execute(int id)
        {
            var temaResponse = new TemaResponse();

            try
            {
                Log.Information("Eliminando el Tema por Id:[{Id}]...", id);
                var toDelete = Repository.SingleOrDefault(tema => tema.Id == id);
                if (toDelete == null)
                {
                    var exception = new Exception($"Tema no encontrado por Id:[{id}].");
                    Log.Error(exception, EXCEPTION_MESSAGE_TEMPLATE, exception.Message);
                    HandleErrors(temaResponse, exception, 404);
                }
                else
                {
                    Repository.Delete(toDelete);
                    temaResponse.StatusCode = 200;
                    Log.Information("Tema eliminado [{NewName}] por Id:[{Id}].", toDelete.Nombre, toDelete.Id);
                }
            }
            catch (Exception exception)
            {
                Log.Error(exception, "Error al elimnar el Tema. [{Message}].", exception.Message);
                HandleErrors(temaResponse, exception);
            }
            return(temaResponse);
        }
コード例 #2
0
        public TemaResponse Execute(int id)
        {
            var temaResponse = new TemaResponse();

            try
            {
                Log.Information("Recuperando tema por Id : [{Id}]", id);

                var tema = Repository.SingleOrDefault(g => g.Id == id);
                if (tema == null)
                {
                    var exception = new Exception($"tema no encontrado por  id : [{id}].");
                    Log.Error(exception, EXCEPTION_MESSAGE_TEMPLATE, exception.Message);
                    HandleErrors(temaResponse, exception, 404);
                }
                else
                {
                    //NOTE: Not sure if I want to do something like AutoMapper for this example.
                    temaResponse.Tema       = tema;
                    temaResponse.StatusCode = 200;
                }
            }
            catch (Exception exception)
            {
                Log.Error(exception, "Error al obtener tema por Id: [{Id}].", id);
                HandleErrors(temaResponse, exception);
            }
            return(temaResponse);
        }
コード例 #3
0
        public TemaResponse Execute(UpdateTemaInput input)
        {
            var temaResponse = new TemaResponse();

            try
            {
                CheckInputValidity(input);

                Log.Information("Actualizando Tema por Id: [{Id}] con nuevo nombre: [{NewTitle}]...", input.Id, input.NombreNuevo);

                var temaToUpdate = Repository.SingleOrDefault(tema => tema.Id == input.Id);
                if (temaToUpdate == null)
                {
                    var exception = new Exception($"No se pudo encontrar el tema con el id: [{input.Id}].");
                    Log.Error(exception, EXCEPTION_MESSAGE_TEMPLATE, exception.Message);
                    HandleErrors(temaResponse, exception, 404);
                    return(temaResponse);
                }

                temaToUpdate.Nombre      = input.NombreNuevo;
                temaToUpdate.Descripcion = input.Descripcion;

                var updatedTema = Repository.Update(temaToUpdate);
                temaResponse.Tema       = updatedTema;
                temaResponse.StatusCode = 200;

                Log.Information("Actualizado con exito el tema con Id: [{Id}] con nuevo nombre [{NewTitle}].", input.Id, input.NombreNuevo);
            }
            catch (Exception exception)
            {
                Log.Error(exception, EXCEPTION_MESSAGE_TEMPLATE, exception.Message);
                HandleErrors(temaResponse, exception);
            }
            return(temaResponse);
        }
コード例 #4
0
        public TemaResponse Execute(CreateTemaInput input)
        {
            var temaResponse = new TemaResponse();

            try
            {
                Log.Information("Creando Tema Con nombre [{NewName}]...", input?.Nombre);
                TemaEntity temaEntity = Mapper.Map <CreateTemaInput, TemaEntity>(input);
                temaEntity.FechaCreacion = DateTime.Now;
                temaResponse.Tema        = Repository.Create(temaEntity);
                temaResponse.StatusCode  = 200;
                Log.Information("Tema creado con el nombre [{NewName}] con el Id: [{Id}]", temaResponse.Tema.Nombre, temaResponse.Tema.Id);
            }
            catch (Exception exception)
            {
                Log.Error(exception, "Error al crear el Tema: [{NewName}].", input?.Nombre);
                HandleErrors(temaResponse, exception);
            }
            return(temaResponse);
        }