Ejemplo n.º 1
0
        public IActionResult UpdateParty(int PartyId, UpdatePartyRequestModel updateParty)
        {
            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")
                    {
                        UpdatepartyResponseModel updatePartyModel = _partyBusiness.UpdateParty(PartyId, updateParty);

                        if (updatePartyModel != null)
                        {
                            if (updatePartyModel.ErrorResponse.ErrorStatus)
                            {
                                message = updatePartyModel.ErrorResponse.Message;
                                return(Ok(new { status, message }));
                            }
                            status  = true;
                            message = "Party Name has Been Updated";
                            PartyUpdatedResponseModel data = updatePartyModel.PartyUpdated;
                            return(Ok(new { status, message, data }));
                        }
                        message = "Unable to update the Party Name";
                        return(Ok(new { status, message }));
                    }
                }
                message = "Invalid Token";
                return(BadRequest(new { status, message }));
            }
            catch (Exception e)
            {
                return(BadRequest(new { e.Message }));
            }
        }
        /// <summary>
        /// It update the party Name
        /// </summary>
        /// <param name="PartyId">Party Id</param>
        /// <param name="updateParty">Party Name</param>
        /// <returns>Update Party Response Model</returns>
        public UpdatepartyResponseModel UpdateParty(int PartyId, UpdatePartyRequestModel updateParty)
        {
            try
            {
                SqlDataReader            reader;
                int                      statusCode, partyUpdatedSuccess;
                UpdatepartyResponseModel updateparty = null;

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

                    SqlParameter PartyPresent = sqlCommand.Parameters.Add("@PartyNamePresentCount", 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();
                    partyUpdatedSuccess = Convert.ToInt32(sqlCommand.Parameters["@PartyNamePresentCount"].Value);
                    statusCode          = Convert.ToInt32(sqlCommand.Parameters["@return_Value"].Value);

                    if (partyUpdatedSuccess > 0)
                    {
                        updateparty = new UpdatepartyResponseModel
                        {
                            ErrorResponse = new ErrorResponseModel
                            {
                                ErrorStatus = true,
                                Message     = "This Name Is already Present"
                            }
                        };

                        return(updateparty);
                    }

                    if (statusCode == 0)
                    {
                        while (reader.Read())
                        {
                            updateparty = new UpdatepartyResponseModel
                            {
                                PartyUpdated = new PartyUpdatedResponseModel()
                                {
                                    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(updateparty);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }