Exemple #1
0
        // This method can return -2 exceptionally that means that "the user to be registered already exists" error.
        public async Task <ReturnModel> NewMember(RegisterModel registerModel) //Return -2 if user exists error has ocurred or MemberId, -1 for db errors.
        {
            Member member = Members.Where(m => m.Username == registerModel.Username || m.Email == registerModel.Email).FirstOrDefault();

            if (member != null)
            {
                return(new ReturnModel {
                    ErrorCode = ErrorCodes.MemberAlreadyExistsError
                });                                                                         // -2
            }
            try
            {
                Member newMember = new Member {
                    Username = registerModel.Username, Email = registerModel.Email, Avatar = registerModel.Avatar
                };

                (newMember.CryptoPassword, newMember.CryptoSalt) = cryptoHelpers.EncryptWithPBKDF2(registerModel.Password);
                await context.Member.AddAsync(newMember); //New member(5 properties) is OK.

                await context.SaveChangesAsync();

                long MemberId = 0; // Use if needed(autoset from db)
                return(new ReturnModel {
                    ErrorCode = ErrorCodes.OK, ReturnedId = MemberId
                });                                                                          //ID
            }
            catch { return(new ReturnModel {
                    ErrorCode = ErrorCodes.DatabaseError
                }); }                                                                   // -1
        }