Невозможно привести объект типа Doctor к типу исключения пользователя - PullRequest
0 голосов
/ 09 июля 2020

Изображение исключения, которое возникает, когда я пытаюсь добавить пользователя на localhost

Это исключение возникает, когда я пытаюсь добавить пользователя на свой localhost из Postman, я должен сказать, когда Я пытаюсь добавить пользователя в облачный веб-API, пользователь успешно добавлен. Я не понимаю, в чем проблема. Я новичок в базовом веб-API. net и MongoDB вместе. AuthenticationUserWebService также наследует несколько методов от CommonAuthenticationWebService

Это модель Doctor

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AuthenticationUserWebService.Models
{
    public class Doctor : AbstractUserModel
    {
        //ajouter plus d'attributs
        public Guid Photo { get; set; }
        public string Mail { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string PhoneNumber { get; set; }
        public List<Kid> Kids { get; set; }
    }
}

Это модель User

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CommonAuthenticationWebService.Models;

namespace AuthenticationUserWebService.Models
{
    public class User: AbstractUserModel
    {
        public string Mail{ get; set; }
        public string FirstName { get; set; }
        public string LastName{ get; set; }
        public List<Kid> Kids { get; set; }
        public DateTime BirthDay { get; set; }
        public int Age { get { return DateTime.Today.Year - BirthDay.Year; } }
        public Gender Gender { get; set; }
        public Guid InterfaceLanguage { get; set; }
        public string PhoneNumber { get; set; }
        public Guid Photo { get; set; }
        
    }
}

MongoDoctorCrudService.cs

using CommonAuthenticationWebService.Models;
using CommonAuthenticationWebService.Services;
using CommonWebServices.Services;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AuthenticationUserWebService.Services
{
    public class MongoDoctorCrudService<TUser> : MongoUserCrudService<TUser> where TUser : AbstractUserModel
    {
        public MongoDoctorCrudService(IUserDatabaseSettings settings) : base((IUserDatabaseSettings)settings)
        {
        }
        public bool ExistDoctor(Doctor doct)
        {
            String collectionName = typeof(Doctor).Name;
            var collection = Db.GetCollection<Doctor>(collectionName);
            var L1 = collection.Find<Doctor>(record => (record.Login == doct.Login)).ToList();
            var L2 = collection.Find<Doctor>(record => (record.Mail == doct.Mail)).ToList();
            if ((L1.Count != 0)||(L2.Count != 0))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public Doctor FindDoctorByEmail(string email)
        {
            String collectionName = typeof(Doctor).Name;
            var collection = Db.GetCollection<Doctor>(collectionName);
            var L = collection.Find<Doctor>(record => (record.Mail == email)).ToList();
            if (L.Count != 0)
            {
                return L[0];
            }
            else
            {
                return null;
            }
        }
        public User FindUserByEmail(string email)
        {
            String collectionName = typeof(User).Name;
            var collection = Db.GetCollection<User>(collectionName);
            var L = collection.Find<User>(record => (record.Mail == email)).ToList();
            if (L.Count != 0)
            {
                return L[0];
            }
            else
            {
                return null;
            }
        }
    }
}

MongoUserCrudService.cs

using MongoDB.Driver;
using CommonAuthenticationWebService.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CommonWebServices.Services;
using CommonAuthenticationWebService.Services;

namespace CommonAuthenticationWebService.Services
{
    public class MongoUserCrudService<TUser> : MongoCrudService where TUser : AbstractUserModel
    {
        public MongoUserCrudService(IUserDatabaseSettings settings):base((IDatabaseSettings) settings)
        {
        }
        public TUser Authenticate(string login, string password)
        {
            String collectionName = typeof(TUser).Name;
            var collection = Db.GetCollection<TUser>(collectionName);
            var L= collection.Find<TUser>(record => (record.Login==login)&&(record.Password == password)).ToList();
            if (L.Count == 0)
            {
                return null;
            }
            else
            {
                return L[0];
            }
        }
        public bool ExistUser(string login)
        {
            String collectionName = typeof(TUser).Name;
            var collection = Db.GetCollection<TUser>(collectionName);
            var L = collection.Find<TUser>(record => (record.Login == login)).ToList();
            if (L.Count != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}

Также пользователь, которого я пытаюсь добавить, также не существует в базе данных.

...