Пример #1
0
        public IActionResult CreateParty(CreatePartyRequestModel createPartyRequest)
        {
            try
            {
                var    admin  = HttpContext.User;
                bool   status = false;
                string message;

                if (admin.HasClaim(c => c.Type == "TokenType"))
                {
                    if (admin.Claims.FirstOrDefault(c => c.Type == "TokenType").Value == "Login")
                    {
                        CreatePartyResponseModel createParty = _partyBusiness.CreateParty(createPartyRequest);

                        if (createParty != null)
                        {
                            if (createParty.ErrorResponse.ErrorStatus)
                            {
                                message = createParty.ErrorResponse.Message;
                                return(Ok(new { status, message }));
                            }
                            status  = true;
                            message = "Party Created Successfully";
                            PartyCreatedResponseModel data = createParty.PartyCreated;
                            return(Ok(new { status, message, data }));
                        }
                        message = "Unable to Create Party";
                        return(Ok(new { status, message }));
                    }
                }

                message = "Invalid Token";
                return(BadRequest(new { status, message }));
            }
            catch (Exception e)
            {
                return(BadRequest(new { e.Message }));
            }
        }
        /// <summary>
        /// It Create a new Party
        /// </summary>
        /// <param name="createPartyRequest">Party Name</param>
        /// <returns>It Return the Reponse Model after creating the new party</returns>
        public CreatePartyResponseModel CreateParty(CreatePartyRequestModel createPartyRequest)
        {
            try
            {
                SqlDataReader            reader;
                int                      statusCode, partyCreatedSuccess;
                CreatePartyResponseModel partyResponse = null;

                using (SqlConnection connection = new SqlConnection(sqlConnection))
                {
                    SqlCommand sqlCommand = new SqlCommand("spParty", connection)
                    {
                        CommandType = System.Data.CommandType.StoredProcedure
                    };
                    sqlCommand.Parameters.AddWithValue("@PartyId", -1);
                    sqlCommand.Parameters.AddWithValue("@Name", createPartyRequest.Name);
                    sqlCommand.Parameters.AddWithValue("@ActionType", "Create");

                    SqlParameter PartyPresent = sqlCommand.Parameters.Add("@PartyPresentCount", System.Data.SqlDbType.Int);
                    PartyPresent.Direction = System.Data.ParameterDirection.ReturnValue;

                    SqlParameter cmdExecuteSuccess = sqlCommand.Parameters.Add("@return_value", System.Data.SqlDbType.Int);
                    cmdExecuteSuccess.Direction = System.Data.ParameterDirection.ReturnValue;

                    connection.Open();

                    reader = sqlCommand.ExecuteReader();
                    partyCreatedSuccess = Convert.ToInt32(sqlCommand.Parameters["@PartyPresentCount"].Value);
                    statusCode          = Convert.ToInt32(sqlCommand.Parameters["@return_Value"].Value);

                    if (partyCreatedSuccess > 0)
                    {
                        partyResponse = new CreatePartyResponseModel
                        {
                            ErrorResponse = new ErrorResponseModel
                            {
                                ErrorStatus = true,
                                Message     = "Party Name Is already Present"
                            }
                        };

                        return(partyResponse);
                    }

                    if (statusCode == 0)
                    {
                        while (reader.Read())
                        {
                            partyResponse = new CreatePartyResponseModel
                            {
                                PartyCreated = new PartyCreatedResponseModel()
                                {
                                    PartyId    = Convert.ToInt32(reader[0]),
                                    Name       = reader[1].ToString(),
                                    CreatedAt  = Convert.ToDateTime(reader[2]),
                                    ModifiedAt = Convert.ToDateTime(reader[3])
                                },

                                ErrorResponse = new ErrorResponseModel()
                                {
                                    ErrorStatus = false
                                }
                            };
                        }
                    }
                }

                return(partyResponse);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }