Пример #1
0
        public ActionResult Create(Product product)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _serviceProduct.Create(product);
                }

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
Пример #2
0
 public Product Post([FromBody] Product product)
 {
     return(_serviceProduct.Create(product));
 }
Пример #3
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            Log.Information("server1.dbo.products démarré à : {Now}", DateTime.Now);

            stoppingToken.Register(() =>
                                   _logger.Information("server1.dbo.products a débuté son arrêt à : {Now}", DateTime.Now));

            var tacheConsommateur = Task.Run(() =>
            {
                using (var serdeProvider = new AvroSerdeProvider(new AvroSerdeProviderConfig {
                    SchemaRegistryUrl = schemaRegistryConfig.SchemaRegistryUrl
                }))
                    using (var consumer = new Consumer <GenericRecord, Envelope>(consumerConfig,
                                                                                 serdeProvider.GetDeserializerGenerator <GenericRecord>().Invoke(true),
                                                                                 serdeProvider.GetDeserializerGenerator <Envelope>().Invoke(false)))
                    {
                        consumer.Subscribe(TOPIC);

                        try
                        {
                            while (true)
                            {
                                try
                                {
                                    var consumeResult = consumer.Consume(stoppingToken);

                                    if (consumeResult.Value.before == null && consumeResult.Value.after != null)
                                    {
                                        //Ajout
                                        product produit = new product()
                                        {
                                            idProduit   = consumeResult.Value.after.id,
                                            name        = consumeResult.Value.after.name,
                                            description = consumeResult.Value.after.description,
                                            weight      = consumeResult.Value.after.weight
                                        };

                                        _serviceProduct.Create(produit);
                                    }

                                    if (consumeResult.Value.before != null && consumeResult.Value.after != null)
                                    {
                                        //MAJ
                                        product produit = new product()
                                        {
                                            idProduit   = consumeResult.Value.after.id,
                                            name        = consumeResult.Value.after.name,
                                            description = consumeResult.Value.after.description,
                                            weight      = consumeResult.Value.after.weight
                                        };

                                        _serviceProduct.Update(produit.idProduit, produit);
                                    }

                                    if (consumeResult.Value.before != null && consumeResult.Value.after == null)
                                    {
                                        //Suppression
                                        _serviceProduct.Remove(consumeResult.Value.before.id);
                                    }

                                    _logger.Information("Key: {Key}, Value: {Value}",
                                                        consumeResult.Message.Key,
                                                        consumeResult.Value);
                                }
                                catch (ConsumeException e)
                                {
                                    _logger.Error("Consommation erreur: {Reason}", e.Error.Reason);
                                }
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            // commit final offsets and leave the group.
                            consumer.Close();
                        }
                    }
            });

            //_logger.Information("service server1.dbo.products arrêté à : {Now}", DateTime.Now);
        }