lors de la création d'une annonce nous ne prenons pas en compte la catégory de l'annonce, maintenant je veux que ce soit le cas, nous devons utilise buildTypesAnnonceDistantselectionner la catégory voulu depuis une combobox: import 'package:flutter/material.dart';
import 'package:supabase_flutter/supabase_flutter.dart';

import 'package:sae_mobile/models/User.dart' as user_model;
import 'package:sae_mobile/models/Builder.dart' as builder_model;

import 'package:sae_mobile/models/queries/local/annonce.dart' as aq;

final SupabaseClient supabaseClient = Supabase.instance.client;

class CreateAnnonce extends StatefulWidget {
  const CreateAnnonce({Key? key}) : super(key: key);

  @override
  State<CreateAnnonce> createState() => _CreateAnnonceState();
}

class _CreateAnnonceState extends State<CreateAnnonce> {
  final TextEditingController _titleController = TextEditingController();
  final TextEditingController _descriptionController = TextEditingController();
  final TextEditingController _dateDebController = TextEditingController();
  final TextEditingController _dateFinController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text('Create Annonce'),
        TextField(
          controller: _titleController,
          decoration: const InputDecoration(labelText: 'Title'),
        ),
        TextField(
          controller: _descriptionController,
          decoration: const InputDecoration(labelText: 'Description'),
        ),
        TextField(
            controller: _dateDebController,
            decoration: const InputDecoration(
                icon: Icon(Icons.calendar_today), labelText: "Enter Date"),
            readOnly: true,
            onTap: () async {
              DateTime? date = await showDatePicker(
                context: context,
                initialDate: DateTime.now(),
                firstDate: DateTime(2000),
                lastDate: DateTime(2100),
              );
              if (date != null) {
                _dateDebController.text = date.toString();
              }
            }),
        TextField(
            controller: _dateFinController,
            decoration: const InputDecoration(
                icon: Icon(Icons.calendar_today), labelText: "Enter Date"),
            readOnly: true,
            onTap: () async {
              DateTime? date = await showDatePicker(
                context: context,
                initialDate: DateTime.now(),
                firstDate: DateTime(2000),
                lastDate: DateTime(2100),
              );
              if (date != null) {
                _dateFinController.text = date.toString();
              }
            }),
        FutureBuilder(
          future: builder_model.Builder.buildUserById(
              supabaseClient.auth.currentUser!.id),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return const CircularProgressIndicator();
            }
            if (snapshot.hasError) {
              return Text('Error: ${snapshot.error}');
            }
            final user = snapshot.data as user_model.User;

            return ElevatedButton(
              onPressed: () async {
                await aq.AnnonceQueries.createAnnonce(
                  user.id,
                  _titleController.text,
                  _descriptionController.text,
                  DateTime.parse(_dateDebController.text),
                  DateTime.parse(_dateFinController.text),
                  1,
                  1,
                  1,
                );

                Navigator.pushNamed(context, '/category');
              },
              child: Text('Create Annonce'),
            );
          },
        ),
      ],
    );
  }
}
import 'package:sae_mobile/models/Objet.dart';
import 'package:sae_mobile/models/TypeAnnonce.dart';
import 'package:sae_mobile/models/annonce.dart';
import 'package:sae_mobile/models/queries/distant/user.dart' as uqd;
import 'package:sae_mobile/models/queries/distant/annonce.dart' as aqd;

import 'package:sae_mobile/models/queries/local/annonce.dart' as aql;
import 'package:sae_mobile/models/queries/local/objet.dart';
import 'package:sae_mobile/models/queries/local/typeAnnonce.dart';

import 'package:sae_mobile/models/User.dart' as user_model;

import 'package:supabase_flutter/supabase_flutter.dart';
import 'package:sae_mobile/models/queries/distant/typeAnnonce.dart' as tqd;

final SupabaseClient supabaseClient = Supabase.instance.client;

/// Classe Builder
///
/// Cette classe permet de construire des objets à partir de données distantes ou locales.
class Builder {
  /// Construit un utilisateur à partir de son id.
  ///
  /// [id] est l'id de l'utilisateur.
  ///
  /// Retourne un objet de type [user_model.User].
  static Future<user_model.User> buildUserById(String id) async {
    final data =
        await uqd.UserQueries.getUserById(id).then((value) => value.first);

    return user_model.User.fromJson(data);
  }

  /// Construit une liste de types d'annonces à partir de données locales.
  ///
  /// Retourne une liste de types d'annonces.
  static Future<List<TypeAnnonce>> buildTypesAnnonce() async {
    final data =
        await TypeAnnoncesQueries.getTypeAnnonces().then((value) => value);

    List<TypeAnnonce> typesAnnonce = [];
    for (var typeAnnonce in data) {
      print('la categorie est : $typeAnnonce');
      typesAnnonce.add(TypeAnnonce.fromJson(typeAnnonce));
    }

    return typesAnnonce;
  }

  static Future<List<TypeAnnonce>> buildTypesAnnonceDistant() async {
    final data =
        await tqd.TypeAnnonceQueries.getTypeAnnonces().then((value) => value);

    List<TypeAnnonce> typesAnnonce = [];
    for (var typeAnnonce in data) {
      print(typeAnnonce);
      typesAnnonce.add(TypeAnnonce.fromJson(typeAnnonce));
    }

    return typesAnnonce;
  }
}
import 'package:sae_mobile/models/annonce.dart';
import 'package:supabase_flutter/supabase_flutter.dart';

final SupabaseClient supabaseClient = Supabase.instance.client;

class AnnonceQueries {
  static Future<String> publishAnnonce(Annonce annonce) async {
    print("publishAnnonce");
    print(annonce.dateDeb.toIso8601String());
    print(annonce.dateFin.toIso8601String());
    print(annonce.titre);
    PostgrestList result = await supabaseClient.from('ANNONCES').insert({
      'titre': annonce.titre,
      'description': annonce.description,
      'dateDeb': annonce.dateDeb.toIso8601String(),
      'dateFin': annonce.dateFin.toIso8601String(),
      'idType': 1,
      'idEtat': 2,
    }).select('id');
    print("result");

    if (result.isEmpty) {
      throw Exception('Failed to create annonce');
    }

    String id = result[0]['id'];

    await supabaseClient.from('PUBLIE').insert({
      'id_a': id,
      'id_user': annonce.auteur.id,
    });

    return id;
  }


  static Future<void> updateAnnonceEtat(String id, int etat) async {
    await supabaseClient.from('ANNONCES').update({'idEtat': etat}).eq('id', id);
  }

  static Future<void> mettreAvis(String id_a, String id_u, String avis) async {
    await supabaseClient.from('AVIS').insert({
      'id_a': id_a,
      'id_user': id_u,
      'avis': avis,
    });
  }

  static Future<PostgrestList> getAnnonceAvis(String id_a) async {
    final response =
        await supabaseClient.from('AVIS').select().eq('id_a', id_a);
    if (response.isEmpty) {
      throw Exception('Failed to get avis');
    }
    return response;
  }
}